Java Static and Final
Java Static and Final
Java
Static Keyword
Java uses static keyword at 4 different places. Lets learn about each of them.
Static Members and Methods There will be times when you will want to define a class
member that will be used independently of any object of that class. Normally, a class
member must be accessed only in conjunction with an object of its class. However, it
is possible to create a member that can be used by itself, without reference to a
specific instance.
To create such a member, precede its declaration with the keyword static. When a
member is declared static , it can be accessed before any objects of its class are
created, and without reference to any object. You can declare both methods and
variables to be static.
The most common example of a static member is main( ). main( ) is declared as static
because it must be called before any objects exist.
Instance variables declared as static are, essentially, global variables. When objects
of its class are declared, no copy of a static variable is made. Instead, all
instances of the class share the same static variable.
Math.java
• They can only directly call other static methods of their class.
• They cannot refer to this or super in any way. (Super keyword is used in
inheritance)
static{
System.out.println("Inside Static Block");
b = a*4;
printData();
}
In the above code, static block will automatically as you launch the program, the
class is loaded and static block is executed. Despite the main being empty, the code
will output the following as static block is still executed.
Code Output
Inside Static Block
3
12
class InnerClass {
// Inner class members
}
}
Nested Static Classes in Builder Design Pattern This kind of class design is
particularly useful in Builder Design Pattern which you will study later as a part of
Low Level Design Course. In short, The Builder Design Pattern is a creational design
pattern that allows you to create complex objects step by step. It's especially useful
when you have an object with many optional parameters or configurations. Here's an
example of how you can implement the Builder pattern in Java:
Suppose you want to create a Person class with optional attributes like name, age,
address, and phone number using the Builder pattern:
@Override
public String toString() {
return "Name: " + name + ", Age: " + age + ", Address: " + address + ", Phone:
" + phoneNumber;
}
}
PersonDemo.java
System.out.println(person1);
System.out.println(person2);
}
}
This allows you to create Person objects with various combinations of attributes while
keeping the code clean and readable.
Final Keyword
The keyword final has three uses. First, it can be used to create the equivalent of a
named constant. The other two uses of final apply to inheritance as discussed below.
1. Final in Variables A field can be declared as final. Doing so prevents its contents
from being modified, making it, essentially, a constant. This means that you must
initialize a final field when it is declared. You can do this in one of two ways:
First, you can give it a value when it is declared. Second, you can assign it a value
within a constructor. The first approach is probably the most common. Here is an
example:
Subsequent parts of your program can now use FILE_OPEN, etc., as if they were
constants, without fear that a value has been changed. It is a common coding
convention to choose all uppercase identifiers for final fields, as this example
shows.
In addition to fields, both method parameters and local variables can be declared
final. Declaring a parameter final prevents it from being changed within the method.
Declaring a local variable final prevents it from being assigned a value more than
once.
The keyword final can also be applied to methods, but its meaning is substantially
different than when it is applied to variables.
2. Using final to Prevent Method Overriding While method overriding is one of Java’s
most powerful features, there will be times when you will want to prevent it from
occurring. To disallow a method from being overridden, specify final as a modifier at
the start of its declaration. Methods declared as final cannot be overridden. The
following fragment illustrates final.
3. Using final to Prevent Inheritance Sometimes you will want to prevent a class from
being inherited. To do this, precede the class declaration with final. Declaring a
class as final implicitly declares all of its methods as final, too. As you might
expect, it is illegal to declare a class as both abstract and final since an abstract
class is incomplete by itself and relies upon its subclasses to provide complete
implementations.
As the
comments imply, it is illegal for B to inherit A since A is declared as final.