Static Keyword in C++ and Java
The static keyword in C++ and Java has different meanings but both are used to define variables and methods with unique properties. In this article, we will explore the static keyword in both programming languages.
Syntax
C++
Static variable
static data_type variable_name;
Static method
static return_type method_name(arguments) {
// code
}
Java
Static variable
static data_type variable_name;
Static method
static return_type method_name(arguments) {
// code
}
Example
C++
#include <iostream>
using namespace std;
class MyClass {
public:
static int static_var;
static void static_method() {
cout << "This is a static method." << endl;
}
};
int MyClass::static_var = 10;
int main() {
cout << MyClass::static_var << endl;
MyClass::static_method();
return 0;
}
Java
public class MyClass {
public static int staticVar = 10;
public static void staticMethod() {
System.out.println("This is a static method.");
}
public static void main(String[] args) {
System.out.println(MyClass.staticVar);
MyClass.staticMethod();
}
}
Output
C++
10
This is a static method.
Java
10
This is a static method.
Explanation
In both examples, we have defined a class with a static variable and a static method.
In C++, we have to define the static variable outside the class definition and initialize it.
In Java, we can define and initialize the static variable inside the class definition.
To access the static variable and method, we use the class name followed by the scope resolution operator (:: in C++ and . in Java).
Use
C++
The static keyword in C++ can be used for the following:
- To create a variable that is shared by all objects of a class.
- To create a method that can be called without creating an object of the class.
- To limit the scope of a variable or function to the current file.
Java
The static keyword in Java can be used for the following:
- To create a variable or method that is shared by all objects of a class.
- To create a variable or method that can be accessed without creating an object of the class.
- To create a static block of code that is executed only once when the class is loaded.
Important Points
- The static keyword in C++ and Java have similar but slightly different meanings.
- In C++, we have to define and initialize a static variable outside the class definition. In Java, we can define and initialize it inside the class definition.
- In C++, we use the scope resolution operator (::) to access a static variable or method. In Java, we use the dot (.) operator.
- The static keyword can be used to create a variable or method that is shared by all objects of a class, or to limit the scope of a function or variable to the current file.
Summary
In summary, the static keyword in C++ and Java is used to define variables and methods with unique properties. In both programming languages, the static keyword can be used to create a variable or method that is shared by all objects of a class, or to limit the scope of a function or variable to the current file.