To define a Global variable in java, the keyword static is used. Java actually doesn’t have the concept of Global variable, it is known as class variable ( static field ). These are the variables that can be used by the entire class.
Example -
public class Library {
private String userName ; // instance variable, specific to a Library
private static int userId ; // class variable
// constructer used to initialise a Student object.
public Library(String userName) {
this.userName = userName ;
}
public static incrementuserId( ) {
userId++;
}
}