What is a Static Keyword in Java?

Related Courses

Static Keyword in Java

In Java, the keywords happen to be the reserved words that we cannot use in the form of Identifiers. And we have in total 57 such in Java. Amongst them is the Static keyword. In this artifact, we are going to discuss deeply the static keyword, and check how we can use it in Java programming. The topics covered in this blog are The Introduction to it, and its various applications like a static variable, static block, static classes, and Static methods. And you can contact us for your Java training. Naresh I Technologies is the number one computer training institute in Hyderabad and among the top five computer training institutes in India. Contact us anytime for your Java training.

What Static keyword is in Java?

When we talk of the static keyword in Java, we mainly deal with memory management. We can use it as a variable, block, method, or as nested classes. Through this keyword, we can share a variable or methods of a class. Generally, we make use of the static to define the constant methods and variables for all class instances. And the main method of the class is always static. You can easily understand why is it so.

To make the static member which can be a block, method, variable, or a nested class, you require preceding the declaration with the static keyword. If we add a static keyword to a member of a class, we can access them before the object of the class gets created, and there is no need for the object reference.

In the Java program, the static keyword happens to be a non-access modifier, and we can make use of it as below:

  • The static Block

  • Static Classes

  • Static method

  • Static variable

And now, we detail each of the above with a proper example.

Static keyword application

We need to first realize how the static block is being used in Java.

  Static Block

For doing the computation for initializing the static variables, you can do the static block declaration which is executed only once, on the class loading. Have a guise at the beneath Java program for understanding the Static block usage.

// Java program for explaining the use of the static block
import java.util.*;
public class Main{
static String a = "Naresh I";// This is static variable
static String b= "Technologies";// This is static variable
static String c = a + b;
// This is static block
static {
System.out.println("Static block");

}
 
public static void main(String[] args)
{
System.out.println("We are inside main");
System.out.println("Welcome to"+" "+a+" "+b);
System.out.println("Welcome: "+" "+c);
}
}

When you implement the above sample, you will get the below output. And you will find in the below output that the static block executes exactly once and that on the class load.

Output:

Static block                                                                                                                  

We are inside main 

Welcome to Naresh I Technologies                                                                                              

Welcome:  Naresh I Technologies

Now you have an idea of how the static block works. Now let us move ahead and check what the static variables are, and how they can help us.

Static Variable

When we place in front of the variable static, then only one copy of the variable is made and it is divided in between all the class level objects. They are essential, the global variables. And generally, each instance of the class has the same copy of variables that are declared static. The static variable gets created at the class level.

Let us now have an example on the above.

// Java program demonstrating the execution of the static variables and the static blocks 

 
import java.util.*;
 
public class Main
{
// this is the static variable
static int a =10;
static int b =20;
static int c = sum(a,b);
 
// static-block
static {
System.out.println("We are in static block");
}
// this is the static-method
static int sum(int a, int b) {
System.out.println("The Sum is: "+ (a+b));
return a+b;
}
 
// This is the main method which is always static 
public static void main(String[] args)
{
System.out.println("The exact c value of c is: "+c);
System.out.println("We are inside the main");
}
}

On execution of the above code, we see that the static block is executed once, and the variables are executed in the order they are defined in the code.

Output:

The exact value of c is: 30                                                                                                       

We are in static block Value of i: 30                                                                                                                

We are inside the main

Now with the above knowledge, we dive deeper into the static keyword and see what are the nested classes and static methods.

Static Method

When we declare the method with a static keyword, that method is known as the static method. And the most appropriate example of the static method happens to be the main() method. And such methods have some restrictions, which are as below:

  • Such methods can call the static methods only directly.

  • And static data are directly accessed through them.

Now let us have a look at the static method through an example:

// see the restrictions on static methods in this program
public class Main
{
// this is static variable
static String a = "Naresh I";
 
// This is an instance variable
String b= "Technologies";
 
// This is static method
static void concatenate()
{
static String a = "Welcome";// This will result in error as a is static
System.out.println("Print"+a);
 
// We cannot make a static reference to nonstatic method a
a = "Narendra"; // This will result in compilation error
 
// We cannot make the static reference to
// concatenate() through the test type
concatenate(); // This will result in compilation error
 
// Super cannot be used in the static context
System.out.println(super.a); // This results in compilation error
}
// This is the instance method
void concatenate()
{
System.out.println("Inside concatenate");
}
 
public static void main(String[] args)
{
// This is the main method
}
}

In this example, you see that there are restrictions on the static method., and how the static methods can use the super in the static context. This is all related to a static method. Now let us have a look at the nested classes.

Static Classes

We can make the class static only if happens to be a nested class. They do not need any references from the Outer class.  And here, the nonstatic members cannot be accessed by the static classes of the outer class. Let us see below how this is executed.

public class MainClass{
private static String a="NareshIT";
//This is a static class
static class NestedClass
{
//This is non static AbstractMethodError
public void PrinttheString(){
System.out.println(a);
}
}
public static void main(String args[]){
MainClass.NestedClass obj = new MainClass.NestedClass();
obj.PrinttheString();
}
}

When this code gets executed, the following output is fetched.

NareshIT

And that is all about the Static keyword in Java. And we have reached the end of our article on the static keyword. However, you now know what is the static keyword, and everything we discussed about it. And do join our Java training classes. We have a lot to offer to you through our training program.

Naresh I Technologies is the number one computer training institute in Hyderabad and among the top five computer training institutes in India. Contact us anytime for your Java training. You can also opt for Java online training, and from any part of the world. And a big package is waiting for you. And all is yours for a nominal fee affordable for all with any range of budget. Let's have a look at what you will get with this Java training package:

  • You need to pay a nominal fee.

  • You can choose any Java certification, as per your skills and interest.

  • You have the option to select from online and classroom training.

  • A chance to study at one of the best Java training institutes in India 

  • We provide Java training in Hyderabad and USA, and no matter in which part of the world you are, you can contact us.

  • Naresh I technologies cater to one of the best Java training in India.

  • And a lot more is waiting for you.

Contact us anytime for your complete Java online training.