What is a Java global variable?
A Java global variable is a variable that is declared outside a method or at the start of a code is a global variable, there are no access restrictions on a global variable. A Java global variable is mostly declared in the first line of code so that it can be used afterward.
How to declare Java global variable?
A Java global variable is technically not allowed, meaning, if we just declare a variable on the first line of code without any other keyword, like in other programming languages, an error will be generated
This shows that, in order to declare a Java global variable the keyword static must be used. Take a look at the code above but with the keyword static.
package main;
import java.util.*;
public class main_class {
// declaring java global variable
static int global_variable=0;
public static void main(String[] args) {
System.out.println(global_variable);
}
}
The output of this code is
0
Now as you can see, there is no error and the variable is accessed by any part of the code.
Use a Java global variable in another class
To show that a Java global variable can be accessed by any part of the code, we will declare the variable in one class like this
package main;
public class global_vars {
public static int variable1 = 10;
public static int variable2 = 20;
}
Now we use this code in another class , create another class, named main class and use the Java global variables in it , shown in the code below
package main;
import java.util.*;
public class main_class {
public static void main(String[] args) {
System.out.println("the global variable is : "+global_vars.variable1);
}
}
The output of this code is
the global variable is: 10
Program 1: Add multiply subtract and divide two Java global variables
In this code, we will declare two Java global variables using the method discussed above and find its sum, difference, product and division, the class that declares the global variables will be as follows :
package main;
public class global_vars {
// java global variable 1
public static int a=30;
// java global variable 2
public static int b=10;
}
Now we make another class and use these variables, the code written below shows how they can be used
package main;
import java.util.*;
public class main_class {
public static void main(String[] args) {
System.out.println("the sum of a and b is : "+ (global_vars.a+global_vars.b));
System.out.println("the difference of a and b is : "+ (global_vars.a-global_vars.b));
System.out.println("the product of a and b is : "+ (global_vars.a*global_vars.b));
System.out.println("the division of a and b is : "+ (global_vars.a/global_vars.b));
}
}
This code output is :
the sum of a and b is : 40
the difference of a and b is : 20
the product of a and b is : 300
the division of a and b is : 3
Program 2: global string manipulation
In this code, some string operations are performed on Java global variable i.e. a string
package main;
import java.util.*;
public class main_class {
// global string
public static String a="this is a java global variable";
public static void main(String[] args) {
System.out.println("the length of string is : "+ a.length());
System.out.println("the UPPERCASE of string is : "+ a.toUpperCase());
System.out.println("the first letter of the string is : "+ a.charAt(0));
System.out.println("are these two strings equal? : "+ (a.equals("this ")));
}
}
The output of this code is
the length of string is : 30
the UPPERCASE of string is : THIS IS A JAVA GLOBAL VARIABLE
the first letter of the string is : t
are these two strings equal? : false
Program 3: Arrays Manipulation Using Java Global Variables
In this program, certain array manipulation operations will be performed on a global character array.
package main;
public class global_vars {
// java global variable
public static char [] char_array= {'a','b','c','d','e','f'};
}
The class that displays the operations will be
package main;
import java.util.*;
public class main_class {
public static void main(String[] args) {
// print
for(int i=0;i<global_vars.char_array.length;i++)
{
System.out.println("array index "+i+1+" contains the element : "+global_vars.char_array[i]);
}
char[]duplicate_array=global_vars.char_array.clone();
System.out.println("is the element 'a' present in the array (0 means no and 1 means yes)? "+Arrays.binarySearch(global_vars.char_array, 'a'));
}
}
This code output shows :
array index 1 contains the element : a
array index 2 contains the element : b
array index 3 contains the element : c
array index 4 contains the element : d
array index 5 contains the element : e
array index 6 contains the element : f
is the element 'a' present in the array (0 means no and 1 means yes)? 0
Program 4: Boolean operations on boolean global variables
In this program, a class named global_vars will declare two boolean variables and the main class will use different boolean operators on these two variables, follow link to know more about Java Boolean Operators
package main;
public class global_vars {
// java global variable
public static final boolean flag1 = false;
public static final boolean flag2=true;
}
The main class code is
package main;
import java.util.*;
public class main_class {
public static void main(String[] args) {
//print
for (int i = 0; i < global_vars.char_array.length; i++) {
System.out.println("array index " + i + 1 + " contains the element : " + global_vars.char_array[i]);
}
char[] duplicate_array = global_vars.char_array.clone();
System.out.println("is the element 'a' present in the array (0 means no and 1 means yes)? " + Arrays.binarySearch(global_vars.char_array, 'a'));
}
}
The output of this code will be
the and of these two boolean variables gives false
the OR of these two boolean variables gives true
Practice Code
Dry run this code for more practice of java global variables, this code may contain some logical errors, find these errors.
package main;
public class global_vars {
// java global variable
public static final boolean flag1 = false;
public static final boolean flag2=true;
public static int a=10;
public static int b=20;
public int c=0;
}
The main class is :
package main;
import java.util.*;
public class main_class {
public static void main(String[] args) {
if (global_vars.flag1 && global_vars.flag2) {
System.out.println("the AND of these two boolean variables gives true");
} else {
System.out.println("the and of these two boolean variables gives false");
}
if (global_vars.flag1 || global_vars.flag2) {
System.out.println("the OR of these two boolean variables gives true");
} else {
System.out.println("the OR of these two boolean variables gives false");
}
}
}
Conclusion
A global variable is a variable that is declared outside a method or at the start of a code is a global variable, there are no access restrictions on a global variable.
In order to declare a Java global variable the keyword static must be used. Take a look at the four basic Java global variable programs to get how they work.
Further Reading
To get further know-how of java global variables take a look at the links below
Class Variables
Initializing Static Fields