Comparison of static keyword in C++ and Java

Static keyword is used for almost same purpose in both C++ and Java. There are some differences though. This post covers similarities and differences of static keyword in C++ and Java.

Static Data Members: Like C++, static data members in Java are class members and shared among all objects. For example, in the following Java program, static variable count is used to count the number of objects created.

filter_none

edit
close

play_arrow

link
brightness_4
code

class Test {
    static int count = 0;
  
    Test() { 
       count++; 
    }    
    public static void main(String arr[]) {
       Test t1 = new Test();
       Test t2 = new Test();
       System.out.println("Total " + count + " objects created");        
    }
}

chevron_right


Output:

Total 2 objects created

Static Member Methods: Like C++, methods declared as static are class members and have following restrictions:

1) They can only call other static methods. For example, the following program fails in compilation. fun() is non-static and it is called in static main()



filter_none

edit
close

play_arrow

link
brightness_4
code

class Main {
    public static void main(String args[]) {   
        System.out.println(fun());
    
    int fun() {
        return 20;
    
}

chevron_right


2) They must only access static data.

3) They cannot access this or super . For example, the following program fails in compilation.

filter_none

edit
close

play_arrow

link
brightness_4
code

class Base {
    static int x = 0;       
}   
  
class Derived extends Base 
{
   public static void fun() {
       System.out.println(super.x); // Compiler Error: non-static variable 
                                  // cannot be referenced from a static context
   }   
}

chevron_right


Like C++, static data members and static methods can be accessed without creating an object. They can be accessed using class name. For example, in the following program, static data member count and static method fun() are accessed without any object.

filter_none

edit
close

play_arrow

link
brightness_4
code

class Test {
    static int count = 0;
    public static void fun() { 
       System.out.println("Static fun() called"); 
    }    
}   
       
class Main
{
    public static void main(String arr[]) {
       System.out.println("Test.count = " + Test.count);        
       Test.fun();
    }

chevron_right


Static Block: Unlike C++, Java supports a special block, called static block (also called static clause) which can be used for static initialization of a class. This code inside static block is executed only once. See Static blocks in Java for details.

Static Local Variables: Unlike C++, Java doesn’t support static local variables. For example, the following Java program fails in compilation.

filter_none

edit
close

play_arrow

link
brightness_4
code

class Test {
   public static void main(String args[]) { 
     System.out.println(fun());
   
   static int fun()  {
     static int x= 10; //Compiler Error: Static local variables are not allowed
     return x--;
   }

chevron_right


Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up