Final Keyword and Static Keyword
Final Keyword and Static Keyword
keyword.
-Mudaliar Krishnakumar.
Topics to be Covered.
1)Static Keyword
a) Static Variable
b) Static Block
c) Static Method
d) Static Class
2)Final Keyword
a) Final Variable
b) Final Method
c) Final Class
Static Keyword
•Shared memory allocation: Static variables and methods are allocated memory space only once
during the execution of the program. This memory space is shared among all instances of the class,
which makes static members useful for maintaining global state or shared functionality.
•Accessible without object instantiation: Static members can be accessed without the need to
create an instance of the class. This makes them useful for providing utility functions and constants
that can be used across the entire program.
•Associated with class, not objects: Static members are associated with the class, not with
individual objects. This means that changes to a static member are reflected in all instances of the
class, and that you can access static members using the class name rather than an object reference.
•Cannot access non-static members: Static methods and variables cannot access non-static
members of a class, as they are not associated with any particular instance of the class.
•Can be overloaded, but not overridden: Static methods can be overloaded, which means that you
can define multiple methods with the same name but different parameters. However, they cannot be
overridden, as they are associated with the class rather than with a particular instance of the class.
1.1 Static Variable
•The static variable can be used to refer to the common property of all objects
(which is not unique for each object), for example, the company name of
employees, college name of students, etc.
•The static variable gets memory only once in the class area at the time of class
loading.
•It is executed before the main method at the time of class loading.
1.3. Static Method
1) If you apply static keyword with any method, it is known as static method.
• A static method belongs to the class rather than the object of a class.
• A static method can be invoked without the need for creating an instance of a class.
• A static method can access static data member and can change the value of it.
There are two main restrictions for the static method. They are:
a) The static method can not use non static data member or call non-static method
directly.
Use the static variable for the property that is common to all objects. For example, in
class Student, all students share the same college name. Use static methods for
changing static variables.
1.4. Static Class
Outer Class:
The class in which nested class is defined is called outer class.
Nested Class:
Java allows us to define a class within a class that is known as a nested class. It may be
static or non-static. The major difference between static and non-static class is that:
•An instance of the static nested class can be created without creating an instance of its outer
class.
•The static and non-static members of an outer class can be accessed by an inner class.
1.4. Static Class
Remember:
•All static classes are nested classes but vice-versa is not true.
•Non-static variable and instance methods cannot be accessed within the static class. If you
try to access a non-static reference from a static field, it throws an error: Cannot make a
static reference to the non-static field.
•We can create static blocks, variables, and methods inside a static class.
•We cannot access the static class if it is inside the static block.
•Initialization: Final variables must be initialized either at the time of declaration or in the
constructor of the class. This ensures that the value of the variable is set and cannot be
changed.
•Performance: The use of final can sometimes improve performance, as the compiler can
optimize the code more effectively when it knows that a variable or method cannot be
changed.
The final keyword can be applied with the variables, a final variable that have no value it is
called blank final variable or uninitialized final variable. It can be initialized in the constructor
only. The blank final variable can be static also which will be initialized in the static block only.
2.1 Final Variable.
Final variables:
• When a variable is declared as final, its value cannot be changed once it has been
initialized.
• This is useful for declaring constants or other values that should not be modified.
2.2 Final Method.
Final methods:
• When a method is declared as final, it cannot be overridden by a subclass.
• This is useful for methods that are part of a class’s public API and should not be modified
by subclasses.
2.2 Final Method
Ans) Yes, final method is inherited but you cannot override it. For Example:
Output : running…
2.3 Final Class
Final classes:
• When a class is declared as final, it cannot be extended by a subclass.
• This is useful for classes that are intended to be used as is and should not be modified or
extended.
Usage 1: 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.
Usage 2: The other use of final with classes is to create an immutable class like the
predefined String class. One can not make a class immutable without making it final.
2.3 Final Class
•Improving performance: The use of final can sometimes help improve performance, as
the Java Virtual Machine (JVM) can optimize code more effectively when it knows that certain
values or references cannot be changed.
•Promoting code reuse: By declaring methods as final, developers can prevent subclasses
from overriding them. This can help promote code reuse and reduce duplication, as
subclasses must use the parent class’s implementation of the method.
•Enhancing security: The use of final can help enhance security by preventing malicious
code from modifying sensitive data or behavior.
THANK YOU