static keyword in java

The static keyword in java is the most important part of the java. I think it is the most common question that asks in interviews and most of the programmer still confused in the static keyword.
The static keyword can be used as a static variable, static method, static block, and static class. To understand the correct meaning and use of static keyword we will create some programs and memory representation.  In this tutorial we will see how to use a static keyword, what are the common rule, and best practices of static keyword.

What is the static keyword in java?

The static keyword is can be used with variable, method, block, and class. It is used to manage the memory in java. The static keyword always belongs to class instead of objects. We will discuss it in detail. Here we will see how we can use the static keyword with variable, method, block, and class.

1. static variable in java
2. static method in java
3. static block in java
4. static class in java

1. Static variable in Java

Here is the table content of the article will we will cover this topic.
1. What is the static variable?
2. Properties of static variable?
3. How to declare a static variable?
4. How to access the static variable?
5. How to use a static variable with example?
6. How does the static variable work?

The static keyword can be used with the variable in java. So whenever we use a static keyword with variable it is called static variable in java. A static variable is associated with class instead of instance/object of the class. We will discuss it in a separate post with different scenarios. Here we will discuss some important properties of static keyword.

  • Static variables are also known as a class variable because they are associated with the class and common for all the objects of a class.
  • Static variables can’t be declared within a method/constructor or within a block of code. It must be declared on the class level.
  • When a class created then one copy of a static variable created in memory. Static variables would be common for all objects of class because a static variable is associated with a class.
  • Static variables are created at the start of program execution and destroyed automatically when execution ends.
  • Initialization of Static Variable is not Mandatory. Its default value is depending on the data type of variable.
  • If we access the static variable through an object the compiler will show the warning message. The compiler will replace the object name to the class name automatically.
  • We can create static variables at class-level only.

NOTE: We don’t need to create an object of that class to access static variables.

Syntax to declare the static variables:

static dataType variableName = value;

Syntax to access static variables:

Class_Name.variable_name
public class Student 
{
  // static variable
  static int count = 0 ;
  public void printStudentRollno()
  {
    System.out.println("Now number of Student is: "+count);
  }
  public static void main(String args[])
  {
    // Access the static variable through class name and assign value
    Student.count = 1;
    // Creating first object
    Student firstStudent = new Student();
    firstStudent.printStudentRollno();

    // Again access the static variable through class name and assign value
    Student.count = 2;
    // Creating second object
    Student secondStudent = new Student();
    secondStudent.printStudentRollno();
  }
}

Output: Now number of Student is: 1
Now number of Student is: 2

Memory representation: Object is always created in heap memory.

static keyword in java

Both objects access the static variable from a static pool. You can read static variables in detail here.

2. Static method in Java

Here is the table content of the article we will cover this topic.
1. What is the static method?
2. How to create a static method?
3. How to use a static method?
4. Scenario 1: When static variables and static methods are in the same class?
5. Scenario 2: When the static variable and static methods are in different classes?
6. Properties of Static Methods in Java?

You can declare a static method by using of static keyword.

1. Static methods are belonging to a class.
2. You can access a static method by using the class name. You do not have the need to create an object.
3. A static method can access only static variables. If you write any non-static variable in a static method, then it will throw an exception at compilation time.

access_modifier static return_Type method_Name()
{
	// body of method
}
public class ExampleStaticMethod
{
	static int x = 5;
	
	public static void printData()
	{
		System.out.println("Value of count = " +x );
	}

	public static void main(String args[])
	{
		ExampleStaticMethod.printData();
	}
}

Output: Value of count = 5

Why a static method is important: Static method can access without any object. They can access directly by class name. The most common example of a static method is main( ) method. If it were a non-static method, JVM creates an object first then call main() method that will lead the problem of extra memory allocation.

3. Static block in Java

Here is the table content of the article will we will cover this topic.

1. What is the static block in java?
2. How to create a static block?
3. How do the multiple static blocks?
4. Static block and method main?
5. Static block and constructor?
6. Static block and instance block (Non-static block)?
i) When it will get executed?
ii) What keyword should be used?
iii) Which type of data can be used?
7. Use of static block in real-time?
i) Perform operation during class loading?
ii) Use to initialize the static final variable?

If you want to do some calculation and initialize the static variables you can use static block. The static block is get executed once when the class is first loaded.

static
{
	//statements
}
public class ExampleStaticMethod
{
	static int c;
	static
	{
		int x = 5;
		int y = 10;
		c = x*y;
	}
	
	public static void main(String args[])
	{
		System.out.println("Calculation = "+ExampleStaticMethod.c);
		
	}
}

Output: Calculation = 50

4. Static class in Java

Here is the table content of the article will we will cover this topic.
1. What is the static class in java?
2. Properties of static class?
3. Declaring a Static Class?
4. How to create an object of static class?
5. Accessing Static Class Members from Static Inner Classes?
6. How does static class work?
7. Key Points to Remember?
8. What is the need of static class?

Leave a Comment