Saturday, March 15.

Header Ads

Colorful+Black+Friday+Discount+Leaderboard+Ad

What is static variables in java?

Muscle+Inc.+%25282%2529

what%20is%20static%20variable%20in%20java

    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

Post Top Ad

Post Bottom Ad