In C++, the keyword static is used to give element unique properties. Static elements are only allocated storage in the static storage area once during the program's lifetime. And they are valid for the duration of the programme. The following are examples of static keywords:
- Functions with static variables
- Objects of a Static Class
- Member who remains constant Class variable
- Class Static Methods
The static keyword in Java is predominantly utilized for memory handling purposes. It can be employed with variables, methods, blocks, and nested classes. Static keyword pertains to the class itself rather than individual instances of the class.
The static can be as follows:
- Varying (also known as a class variable)
- Method (also known as a class method)
- Block
- Class hierarchy
In both C++ and Java, the static keyword plays a similar role, yet there exist certain differences. This article delves into the comparison and differentiation of the static keyword in C++ and Java.
Static Keyword Similarities between C++ and Java:
- Both languages support the definition of static data members.
- Both languages support the definition of static member functions.
- It is possible to gain easy access to static members without having to create any objects.
Differences in Static Keywords between C++ and Java:
| C++ | JAVA |
|---|---|
| Static blocks are not supported in C++. | Java provides static blocks (also called static clause). It is used to perform static class initialization. |
| It is possible to declare static local variables. | Static Local Variables are incompatible. |
The following points are discussed in detail below:
1) Static Data Members: Similar to C++, static data members in Java are class members that are accessible by all instances. In the Java program below, the static variable "count" is employed to keep track of the total number of objects instantiated.
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 " );
}
}
Output
Total 2 objects created
Static Member Methods: In both C++ and Java, static member functions can be implemented. These methods are class members that come with specific limitations:
a. Solely static methods can be employed. For instance, the compilation of the subsequent program encounters an error. The function fun is non-static and invoked within the static main.
class Main {
public static void main ( String args[] )
{
System.out.println ( fun() );
}
int fun() { return 20; }
}
b. They are only permitted to access static data.
c. They do not have access to "this" or "super". For instance, the compilation of the subsequent program encounters an error.
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 );
}
}
Just as in C++, static data members and static methods can be accessed without instantiating an object, as they are reachable through the class names. In the provided program, for instance, the static data member "count" and the static method "fun" are accessed without requiring an object to be created.
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();
}
}
Output
Test.count = 0
Static fun() called
In Java, there exists a unique block known as a static block (or static clause) which serves the purpose of initializing static classes. The code enclosed within this static block runs only once. For further details, refer to Static blocks in Java.
4) Local Static Variables: In contrast to Java, C++ allows the use of static local variables. For instance, compiling the below Java program results in an error.
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--;
}
}