Static Keyword In C++ And Java

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 primarily used for memory management. The static keyword can be used with variables, methods, blocks, and nested classes. The static keyword is associated with the class rather than an instance 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 serves nearly the same function. However, there are some distinctions. This post compares and contrasts 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: Static data members in Java, like in C++, are class members that are shared by all objects. In the following Java programme, for example, the static variable count is used to count the number of objects created.

Example

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

Output

Total 2 objects created

2) Static Member Methods: Static member functions can be defined in C++ and Java. Static methods are class members with the following restrictions:

a. can only use static methods. For example, the compilation of the following programme fails. fun is a non-static function that is called in static main .

Example

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 example, the compilation of the following programme fails.

Example

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. Just like in C++, static data members and static methods can be accessed without the need to create an object. They are accessible via the class names. In the following programme, for example, static data member count and static method fun are accessed without the use of an object.

Example

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

Output

Test.count = 0
Static fun() called

3) Static Block: Unlike C++, Java has a special block called static block (also known as static clause) that can be used for static class initialization. This code within the static block is only executed once. For more information, see Static blocks in Java.

4) Static Local Variables: Unlike Java, static local variables are supported in C++. For example, the compilation of the following Java programme fails.

Example

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--;
	}
}

Input Required

This code uses input(). Please provide values below: