What is a Static Keyword in Java? - NareshIT
Naresh i Technologies
Only Institute to offer the 'Most Comprehensive eLearning Platform to suit the self-learning needs of all CMS and LMS
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:
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:
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:
Contact us anytime for your complete Java online training.
FAQ'S
The static keyword in Java is a modifier used for memory management. It indicates that a particular field, method, or block belongs to the class rather than any specific instance. As a result, static members can be accessed without creating an object of the class.
The static keyword can be applied to:
Variables: Shared among all objects of the class.
Methods: Can be called without an object instance.
Blocks: Executes when the class is loaded.
Nested Classes: Used for classes declared inside another class.
Memory Efficiency: Static members are allocated memory once during class loading.
Global Access: Can be accessed directly using the class name.
Utility Functions: Ideal for utility or helper methods like Math.max().
For More Details Visit : Java online training
Register Here For New Batches : https://tally.so/r/mKo9OV
Register For Free Demo on UpComing Batches : https://nareshit.com/new-batches