• The class level members which have static keyword in their definition are called static members.

Types of Static Members:

  •  Java supports four types of static members
  1. Static Variables
  2. Static Blocks
  3. Static Methods
  4. Main Method (static method)

  • All static members are identified and get memory location at the time of class loading by default by JVM in Method area.
  • Only static variables get memory location, methods will not have separate memory location like variables.
  • Static Methods are just identified and can be accessed directly without object creation.

1.Static Variable:

  • A class level variable which has static keyword in its creation statement is called static variable.

  1. package com.instanceofjava;
  2. class Demo{
  3.  
  4. static int a=10;
  5. static int b=20;
  6.  
  7. }

  • We can not declare local variables as static it leads to compile time error "illegal start of expression".
  • Because being static variable it must get memory at the time of class loading, which is not possible to provide memory to local variable at the time of class loading.

  1. package com.instanceofjava;
  2. class Demo{
  3.  
  4. static int a=10;
  5. static int b=20;
  6.  public static void main(String [] args){
  7.  
  8.    //local variables should not be static
  9.  static int a=10;// compile time error: illegal start of expression

  10. }


  • All static variables are executed by JVM in the order of they defined from top to bottom.
  • JVM provides individual memory location to each static variable in method area only once in a class life time.

Life time and scope:

  •  Static variable get life as soon as class is loaded into JVM and is available till class is removed from JVM or JVM is shutdown.
  • And its scope is class scope means it is accessible throughout the class.

  1. package com.instanceofjava;
  2. class StaticDemo{
  3.  
  4. static int a=10;
  5. static int b=20;
  6.  
  7.  public static void main(String [] args){
  8.  
  9.    System.out.println("a="+a);
  10.    System.out.println("a="+b);
  11.    show();

  12.  }
  13.  
  14. public static void show(){
  15.  
  16.    System.out.println("a="+a);
  17.    System.out.println("a="+b);
  18. }

  19. }

static methods JVM architecture

Duplicate Variables:

  • If multiple variables are created with same name are considered as duplicate variables.
  • In the same scope we can not create multiple variables with same name.
  • If we create it leads to compile time error "variable is already defined".
  • Even if we change data type or modifier or its assigned value we can not create another variable with same name.

  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4.  
  5. static int a=10;
  6. int a=30;// Compile time error 
  7.  
  8. }

  • But it is possible to create multiple variables with same name in different scopes.

  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4. static int a=10;
  5. int a=30;// Compile time error
  6.  
  7. public static void main(String [] args){
  8.   
  9. // it is allowed to define "a" in this method.
  10. int a=20;
  11.  
  12. }
  13. }

Shadowing:

  • It is possible to create local variables or parameters with same variable name.
  • The concept is called shadowing . It means local variable is a shadow of class level variable.
  • It means when you access it in side method , you will get local variables value but not  from class level.

  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4. static int a=10;

  5.  
  6. public static void main(String [] args){
  7.   

  8.  int a=20;
  9.  System.out.println("a="+a); // prints 20

  10. }
  11. }

 Local preference:

  • When we call a variable compiler and JVM will search for its definition in that method first, if its definition is not found in that method then will search for its definition at class level.
  • If its definition not found at class level also then compiler throws error: can not find symbol.
  • This phenomenon is called local preference.


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4. static int a=10;

  5.  
  6. public static void main(String [] args){
  7.  
  8.  System.out.println("a="+a); // prints 10 : method level no definition so access class level

  9.  int a=20;
  10.  System.out.println("a="+a); // prints 20 :method level variable found

  11. }
  12. }


  • By using class name we can get class level variables value.


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4. static int a=10;

  5.  
  6. public static void main(String [] args){
  7.  
  8.  System.out.println("a="+a); // prints 10 : method level no definition so access class level

  9.  int a=20;
  10.  System.out.println("a="+a); // prints 20 :method level variable found

  11.  System.out.println("a="+StaticDemo.a); // prints 10 : class level variable
  12.  
  13. }
  14. }


Order of execution of static variables and main method:

  • First all static variables are executed in the order they defined from top to bottom then main method is executed.
Example Program:


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4. static int a=m1();
  5.  
  6. static int m1(){
  7.  
  8. System.out.println("variable a is created");
  9. return 10;
  10.  
  11.  
  12. static int b=m2();
  13.  
  14. static int m2(){
  15.  
  16. System.out.println("variable b is created");
  17. return 20;

  18.  
  19. public static void main(String [] args){
  20.   
  21.  System.out.println("in main method");
  22.  System.out.println("a="+a);
  23.  System.out.println("b="+b);


  24.  
  25. }
  26. }

Output:
  1. variable a is created
  2. variable b is created
  3. in main method
  4. 10
  5. 20

  • we can access static fields using objects but static fields should be accessed in a static way because static fields are class level so even if we change field value using particular object that will change the original value 


  1. package com.instanceofjava;
  2.  
  3. public class StaticDemo {
  4.  
  5.  static int x=10;
  6.  
  7. public static void main(String[] args) {
  8.  
  9.  StaticDemo obj=new StaticDemo();
  10.  
  11. System.out.println(x);
  12.  
  13. obj.x=30;
  14.  
  15. System.out.println(x);
  16.  
  17. StaticDemo obj2=new StaticDemo();
  18.  System.out.println(obj2.x); 
  19.  
  20. System.out.println(StaticDemo.x); // Recommended way to access static variables

  21. }
  22. }

Output:

  1. 10
  2. 30
  3. 30
  4. 30

Final Static Variables:

  • Final static variables are constants.
  • Declaration itself we need to assign value to the final static variables otherwise compiler error will come: "The blank final field  may not have been initialized".
  • And we can not change this value if we try then compiler error will come: The final field  cannot be assigned.


  1. package com.instanceofjava;
  2. class Demo{
  3.  
  4.  final static int MAX_VALUE=10;

  5.  public static void main(String [] args){
  6.  
  7.    System.out.println(MAX_VALUE);

  8. }



Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

2 comments for Static Members in java

Select Menu