Static keyword in C++ vs Java
Both in C++ and Java, the static keyword is employed for essentially the same function. But there are some variations. The static keyword's similarities and differences between C++ and Java are discussed in this article.
What do we mean by the keyword Static ?
In the C++ programming language, static is used to help an element with some strict characteristics. Static elements are permitted to be given the storing location only in the static storage region once during the program's entire lifetime. And they're justifiable as long as the program is being executed.
In Java, memory management is the main application for the static keyword. Blocks, nested classes, variables, methods, and blocks can all be utilised using the static keyword. Instead of referring to a class instance, the static keyword is connected to the class.
Java and C++ Share Similarities for Static Keyword :
- Both languages allow for the definition of static data members.
- In both the programming languages, static member methods can be defined.
- Without constructing any objects, it is simple to access static members.
C++ and Java's differences for static keywords :
C++ :
- Static blocks are not supported by C++.
- Declaring Static Local Variables is possible.
Java :
- Java allows for static blocks (also known as static clause). It is often in use for static initialization of a class.
- The use of static local variables is not permitted.
Below, the details of the aforementioned points are discussed :
1.Static Data Members:
Static data members in Java, just like they are in the C++ programming language, are shared among all the objects and are class members. For instance, the static variable count is often used to tally the number of objects produced in the Java programme below.
Example :
class Testing {
static int counting = 0;
Testing() { counting++; }
public static void main(String arr[])
{
Testing T1 = new Testing();
Testing T2 = new Testing();
Testing T3 = new Testing();
Testing T4 = new Testing();
Testing T5 = new Testing();
System.out.println("In Total " + counting
+ " objects are created");
}
}
Output :
In Total 5 objects are created
2. Static Member Methods :
Static member methods can be expressed in the C++ as well as Java programming languages. The following limitations apply to methods that are class members and are specified as static :
A) They are limited to calling static methods. As an illustration, the compilation of the programme mentioned below fails. funct() is not static and is used in the static main() function.
Example :
class Main {
public static void main(String argmnts[])
{
System.out.println(fun());
}
int funct() { return 123; }
}
B) Only static data should be accessed.
C) They are unable to access this or super. As an illustration, the compilation of the following software fails.
Example :
class BaseClass {
static int a = 0;
}
class DerivedClass extends BaseClass {
public static void funct()
{
System.out.println(super.a);
}
}
Output :
Compiler Error: non-static variable can’t be referenced from a static context
D) Static methods and data members can be accessible without first generating an object, much like in C++. The class names can be used to get to them. As an illustration, the static function funct() and the static data member counting are accessible without the use of an object in the following programme.
Example :
class Testing {
static int counting = 0;
public static void funct()
{
System.out.println("\nStatic funct() is called");
}
}
class Main {
public static void main(String ar[])
{
System.out.println("Testing.counting = " + Testing.counting);
Testing.funct();
}
}
Output :
Testing.counting = 0
Static funct() is called
3. Static Block :
With exception of C++, Java allows a specific type of block known as a static block (sometimes known as a static clause), which is used for a class' static initialization. The static block's code is only invoked once.
4. Static Local Variables :
C++ allows the use of static local variables, in contrast to Java. For instance, the compilation of the following Java application fails :
Example :
class Testing {
public static void main(String argmnts[])
{
System.out.println(funct());
}
static int funct()
{
static int a = 97;
return a--;
}
}
Output :
Compiler Error: Static local variables are not allowed