What is static variables in java?
What is static variables in java?
- · Static variable are class members.
- · Static variable are created inside
the class outside the method by using ‘static’ keyword.
- · It has global access . we can use
anywhere in the program.
- · It is not mandatory to initialize .
If we don’t initialize , depending upon data type auto initialization takes
place.
- · We need not have to create object for
accessing it.
public class A{
static int x=10;
public static void main(String []args){
System.out.println(A.x);
}
}
Static variable value can be changed as shown in the below
example
public class A{
static int x=10;
public static void main(String []args){
System.out.println(A.x);
A.x =1000;
System.out.println(A.x);
}
}
No comments