After learning about Java constructors, In this tutorial, we'll be discussing another important aspect of Java, known as the static keyword. What is static keyword in Java? and the Uses of static keyword?
In Java, keywords are the special words that are reserved by the language for some special tasks, and cannot be used as identifiers. Each keyword has its own role to play. And one of the most used keywords in Java is static. We have been using this static keyword, in every Java program. But in this tutorial, we will thoroughly study it.
What is static in Java?
Java static is a special type of reserved word that is particularly used for memory management. That means, for any entity that is to be shared between the different instances of a Java Class. We can use the static keyword with variables, methods, blocks of statements as well as with classes(only nested classes).
Syntax : static <member-name>
To create a static member, we just need to add the keyword static before it. And then it would be even accessible without the use of any object. But this is the case, only with other static members.
For non-static members, we have to use instances of the class.
Use of static keyword in Java
As mentioned above, the static keyword can be used with
- variables
- methods
- block of statements
- classes(only nested classes)
Now, we'll be studying all these members in detail.
Java static variable
When we add the prefix 'static' in front of any ordinary variable name, then it is addressed as a static variable. Static variables can be accessed by any instance of the class without the use of an object, i.e a single copy of the variable is shared amongst all other instances.
Hence, making them globally accessible but only at the class level. It can be directly accessed using the class name only.
A static variable gets memory allocated or initialized only once when the class is loaded or at the time of execution.
Example: In this example, you can see, the String variable declared as static is accessible directly from the static main() method. Whereas to access the non-static String s1, we have to create an instance of the class.
class A {
public static String s3 = "Another Static variable";
}
public class staticVariable {
String s1 = "It's non-static";
static String s2 = "It's static";
public static void main(String[] args) {
staticVariable object1 = new staticVariable();
System.out.println(object1.s1);
System.out.println(s2);
System.out.println(staticVariable.s2);
System.out.println(A.s3);
}
}
OUTPUT
It's non-static
It's static
It's static
Another Static variable
We can also access the static variable from another class, by specifying the class name before the static-variable-name.
If we try to access a non-static member through a static method in the same above code, then a compile-time error is thrown by the compiler.
public class staticVariable {
String s1 = "It's non-static";
static String s2 = "It's static";
public static void main(String[] args) {
System.out.println(s1);
System.out.println(s2);
}
}
OUTPUT
Unresolved compilation problem:
Cannot make a static reference to the non-static field s1
Java static method
Any Java method with prefix static is known as a static Java method. static methods, like the other members, also belong to the class, rather than a particular instance of the class.
Such methods can be invoked without specifying the name of the instance.
They are very much useful in memory management, but the only limitation they provide is, they can only directly call other static methods and static variables, whereas, for non-static members, objects need to be used.
Example: In this program, we can directly call the static method, without creating an instance of the class.
public class staticMethod {
public static void main(String[] args) {
printMethod();
}
public static void printMethod() {
System.out.println("This is a static method");
}
}
OUTPUT
This is a static method
If we want to call a non-static method from the same class, then we have to create an instance of the class, and then access the method.
public class staticMethod {
public static void main(String[] args) {
staticMethod object = new staticMethod();
object.method1(); //Calling the non-static method
printMethod(); //Calling the static method
}
public static void printMethod() {
System.out.println("This is a static method");
}
public void method1() {
System.out.println("This is a non-static method");
}
}
OUTPUT
This is a non-static method
This is a static method
We can also call the static methods, from inside the non-static methods, as shown in the example below.
public class staticMethod {
public static void main(String[] args) {
staticMethod object = new staticMethod();
object.method1();
}
public static void printMethod() {
System.out.println("This is a static method");
}
public void method1() {
System.out.println("This is a non-static method");
printMethod();
}
}
OUTPUT
This is a non-static method
This is a static method
But to call a non-static method, from a static one, we have to create an instance of that class there also. Then the non-static method would be accessible.
Java static block
static blocks are blocks of statements that are executed at the execution/starting of the program, even before the main() method. They are used for initializing static variables.
We can have multiple static blocks also. They'll be executed in the same sequence in which they are defined.
Example: On executing the following program, the static blocks would be executed first, before the execution of the main() method. And they are executed in the same order in which they are written.
public class staticBlock {
static { //1st block
System.out.println("Executes First");
}
public static void main(String[] args) {
System.out.println("In the Main() method");
}
static { //2nd block
System.out.println("Executes Second");
}
}
OUTPUT
Executes First
Executes Second
In the Main() method
Java static class
A Java class can only be declared as static, if it is a nested class, that means a class within another class. Declaring an inner class as static doesn't require the reference of the outer class. But it can only access the static members of the outer class, not the non-static ones.
Example: Here the innerClass is a nested class of the class staticClass, and to access its methods, we need to have either a complete instance or the name of the nested class(in which it is declared) only.
public class staticClass {
static class innerClass {
static String s = "Static class";
static void methodA() {
System.out.println(s);
}
}
public static void main(String[] args) {
staticClass.innerClass object = new staticClass.innerClass();
object.methodA();
innerClass.methodA();
}
}
OUTPUT
Static class
Static class