Comparison of static keyword in C++ and Java

22 Nov, 2021
Static keyword is used for almost the 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. 
Similarities between C++ and Java for Static Keyword:
Static data members can be defined in both languages.
Static member functions can be defined in both languages.
Easy access of static members is possible, without creating some objects.
Differences between C++ and Java for Static Keyword:
C++
Java
The above points are discussed are in detail below:
1. 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, the static variable count is used to count the number of objects created.
Java
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");    }}
Total 2 objects created
2. Static Member Methods: In C++ and Java, static member functions can be defined. Methods declared as static are class members and have the following restrictions:
A) They can only call other static methods. For example, the following program fails in the compilation. fun() is non-static and it is called in static main().
Java
class Main {    public static void main(String args[])    {        System.out.println(fun());    }    int fun() { return 20; }}
B) They must only access static data.
C) They cannot access this or super. For example, the following program fails in the compilation.
Java
class Base {    static int x = 0;} class Derived extends Base {    public static void fun()    {         // Compiler Error: non-static variable        // cannot be referenced from a static context        System.out.println(super.x);    }}
D) Like C++, static data members and static methods can be accessed without creating an object. They can be accessed using the class names. For example, in the following program, static data member count and static method fun() are accessed without any object. 
Java
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();    }}
Test.count = 0
Static fun() called
3. 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 the static block is executed only once. See Static blocks in Java for details.
4. Static Local Variables: Unlike Java, C++ supports static local variables. For example, the following Java program fails in the compilation. 
Java
class Test {    public static void main(String args[])    {        System.out.println(fun());    }    static int fun()    {         // Compiler Error: Static local        // variables are not allowed        static int x = 10;        return x--;    }}
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
anshikajain26
C++-Static Keyword
Static Keyword
C++
Java
Java
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.