Here over this article let's explore the keywords static and final. It is able to create methods or variables using these keywords. We will discuss all the advantages and disadvantages of these keywords.
What is a static keyword?
In Java, if we want to access class members, we must first create an instance of the class then we can call the class members by using the instance variable. But there will be situations where we want to access class members without creating any variables.
In such situations, we can use the
static keyword in Java. If we want to access class members without creating an instance of the corresponding class, we need to declare the class members static.
In java most of the utility class members are static. Here are some examples of static utility classes.
java.util.Objects: Contains static utility operations for object method.
java.util.Collections: This class consists exclusively of static methods that operate on or return collections.
Where all we can use static keyword?
We can use static keywords in four places. Which are
Let's explore each of the above in detail.
Static methods are also called class methods. It is because a static method belongs to the class rather than the object of a class. Also, we can invoke static methods directly using the class name.
In the above example, the method
add is static. So that it can call directly with the class name. No need to create a new instance for the
StaticExample class. But the
method multiply is not static. So for the non-static method, it should need to create a new instance of
In Java, when we create objects of a class, then every object will have its own copy of all the variables of the class. For example,
However, if we declare a variable static, all objects of the class share the same static variable. It is because like static methods, static variables are also associated with the class. And, we don’t need to create objects of the class to access the static variables. For example,
In the above example, the
normalVariable is a class variable and
staticVariable is a static variable. Here if you declare like below
firstExample.staticVariable = “Hello”
This is similar to accessing static variable with class name
VariableExample.staticVariable = “Hello”
Here the static variables are class-level variables. So if you are accessing static variables via object instance variable then it actually reflected on the corresponding class level static variable. So in all instances, static variables have the same value. But for instance variable, for every object instance, it keeps a separate value. So the output of the above code snippet follows.
normalVariable: nullstaticVariable: Hello
Static variables are rarely used in Java. Instead, the static constants are used. These static constants are defined by
static finalkeyword and represented in uppercase. This is why some people prefer to use uppercase for static variables as well.
Here we can see that we have used a static block with the syntax:
static blocks are used to initialize the static variables. The static block is executed only once when the class is loaded in memory. The class is loaded if either the object of the class is requested in code or the static members are requested in code.
A class can have multiple static blocks and each static block is executed in the same sequence in which they have been written in a program.
The output of the above code snippet is
staticVariable updated with the second static block value.
Nested Static Class
In Java, we can declare a class inside another class. Such classes are known as nested classes. Nested classes are of 2 types:
A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.
An inner class, by default, has an implicit reference to an object of the outer class. If you instantiate an object of this from the code of the outer class, this is all done for you. If you do otherwise you need to provide the object yourself.
A static inner class does not have this. So we can say If the inner class has no reason to access the outer one, you should make it static by default.
In the above example, you can see the inner class
MyInnerClass can access all the methods and variables of the outer class including the private variables. But in the case of the static inner class, it is not able to access any outer class methods or variables in the static inner class.
Where the static variables classes are stored in Java Memory?
Static methods and variables have been Stored in the permgen space before the 8th version of java. But, now they have introduced a new memory space called metaspace now this is the place where all those name and fields of the class, methods of a class with the bytecode of the methods, constant pool, JIT optimizations etc are stored. The reason for removing permgen in java 8.0 is It is very hard to predict the required size of permgen.
What is the use of final keyword?
In Java, the
final keyword can be used while declaring an entity. Using the final keyword means that the value can’t be modified in the future. It is able to apply the final keyword for variable, method and class.
final variable: Is for creating constant values.
final method: which prevent method overriding
final class: Which prevent inheritance.
What is final variable and when we can use it?
When a variable is declared with
final keyword, its value can’t be modified, essentially, a constant. This also means that you must initialize a final variable. If the final variable is a reference, this means that the variable cannot be re-bound to reference another object, but the internal state of the object pointed by that reference variable can be changed i.e. you can add or remove elements from a final array or final collection. It is good practice to represent final variables in all uppercase, using underscore to separate words.
There are three ways to initialize a final variable :
- You can initialize a final variable when it is declared. This approach is the most common. A final variable is called a blank final variable if it is not initialized while declaration. Below are the two ways to initialize a blank final variable.
- A blank final variable can be initialized inside an instance-initializer block or inside the constructor. If you have more than one constructor in your class then it must be initialized in all of them, otherwise, a compile-time error will be thrown.
- A blank final static variable can be initialized inside the static block.
When to use a final variable :
The only difference between a normal variable and a final variable is that we can re-assign value to a normal variable but we cannot change the value of a final variable once assigned. Hence final variables must be used only for the values that we want to remain constant throughout the execution of the program.
Where all we can use final classes?
When a class is declared with final keyword, it is called a final class. A final class cannot be extended(inherited). There are two uses of a final class :
One is definitely to prevent inheritance, as final classes cannot be extended. For example, all Wrapper Classes like Integer, Float etc. are final classes. We can not extend them.
It is not able to extend with the final class. If you do so then the compiler throws the following exception.
When we can use the final methods?
When a method is declared with the final keyword, it is called a final method. A final method cannot be overridden. The Object class does this — a number of its methods are final. We must declare methods with the final keyword for which we required to follow the same implementation throughout all the derived classes. The following fragment illustrates the final keyword with a method:
In the above example, it is trying to override the final method in the parent class. Java is not allowed this. Java throws the following exception if you try to do so.
Hope you got a good understanding of final and static keywords from this article.