Module 2
Understanding Static , Introducing Final and Introducing
Nested Inner
Static
• Static" means not moving, changing, or active, and is often used in programming to indicate that a
member (like a variable or method) belongs to a class itself rather than to any specific instance or
object of that class.
• This allows members to be accessed without creating an object and is crucial for things like utility
functions or for variables that are shared across all objects of a class.
• The static keyword in Java is especially helpful when you need to define utility methods, constant
values, or shared resources that remain the same for all instances of a class.
• When you declare a member (variable or method) as static, it means:
• It belongs to the class itself, not to any specific object
• There’s only one copy shared across all instances
• It can be accessed without creating an object of the class
• It’s loaded into memory when the class is loaded, not when objects are created
Types of Static Members in Java
The static keyword can be applied to four main
components:
• Static Variables
• Static Methods
• Static Blocks
• Static Nested Inner classes
Static variables
A static variable is also known as a class variable. It is shared among all instances of the class and
is used to store data that should be common for all objects.
Example : interest rate, institution name , School name and so on.
class Student {
int rollNo; // instance variable
String name; // instance variable
// static variable
static String Training_Center
= "GFG";
Student(int r, String n){
rollNo = r;
name = n;
}
void display() {
[Link](rollNo + " " + name + " " + Training_Center);
}
}
public class GFG{
public static void main(String[] args){
Student s1 = new Student(101, "Ravi");
Student s2 = new Student(102, "Amit");
[Link]();
[Link]();
}
}
OUTPUT
101 Ravi GFG
102 Amit GFG
Feature Instance Variable Static Variable
Belongs to Object Class
Number of
One per object Only one (shared)
Copies
Example rollNo, name Training_Center
Access using [Link] [Link]
Static Methods
• A static method belongs to the class rather than to any object. It can be called directly using the
class name.
• You can call a static method without creating an object of the class.
[Link]();
• Static methods are mainly used for utility tasks or operations that don’t need data from any
object.
• Can Access static members
Inside a static method, you can use:
static variables
static methods
• ❌ Cannot access instance variables directly
❌ Cannot call instance methods directly
• Cannot use this or super ,Because static methods do not belong to any object.
class Student {
static String center = "GFG";
static void showCenter() { // Static Method
[Link](center);
}
}
public class Test {
public static void main(String[] args) {
[Link](); // calling static method
}
}
Static Blocks
A static block in Java is a block of code that runs only once when the class is
first loaded into memory.
It is mainly used to initialize static variables or perform setup tasks before
anything else runs.
Static blocks run automatically, even before the main() method starts.
Syntax:
static {
// code to execute
}
class Student {
int rollNo;
String name;
// static variable (shared by all students)
static String trainingCenter;
// static block (runs first when class loads)
static {
trainingCenter = "GFG Training Center";
[Link]("Static Block Executed: Training center initialized");
}
// constructor
Student(int r, String n) {
rollNo = r;
name = n;
}
// static method (can be called without object)
static void changeCenter(String newCenter) {
trainingCenter = newCenter;
}
// instance method
void display() {
[Link](rollNo + " " + name + " " + trainingCenter);
}
}
public class Main {
public static void main(String[] args) {
// calling static method before creating objects
[Link]("Code Academy");
// creating objects
Student s1 = new Student(101, "Ravi");
Student s2 = new Student(102, "Amit");
// displaying details Output
[Link]();
[Link](); Static Block Executed: Training center initialized
} 101 Ravi Code Academy
} 102 Amit Code Academy
Static Nested Classes
A static nested class is a class declared as static inside another class. It can be accessed without
creating an object of the outer class.
class Outer {
static String institute = "GFG";
// static nested class
static class Student {
int rollNo;
String name;
Student(int r, String n) {
rollNo = r;
name = n;
}
void display() {
// Can access only static data of Outer
[Link](rollNo + " " + name + " " + institute);
}
}}
public class Demo {
public static void main(String[] args) {
// object of static nested class → NO Outer object needed
[Link] s = new [Link](101, "Ravi");
[Link]();
}
}
OUTPUT
101 Ravi GFG
Feature Static Variable Static Method Static Block
Belongs to Class Class Class
No memory
Only one copy shared by all
Memory No memory per object (executes only
objects
once)
When executed When class loads When called When class loads
Access using [Link] [Link]() Runs automatically
Can access static
Can access Only static data directly Only static members
data
Initializing static
Used for Storing common data Utility operations
data
In Student static block that
trainingCenter changeCenter()
example prints message
When to Use the Static Keyword in Java
The static keyword in Java is used when you want to define something that should belong to the
class itself and not to individual objects. This is helpful when certain data or behavior stays the same
for all objects of a class. Using static reduces memory usage, improves performance, and makes the
code easier to manage.
•When the value is common for all objects: Use a static variable when all objects of a class should
share the same value (e.g., company name, college name).
•When the method does not depend on object data: Use a static method for tasks that don’t need
to use instance variables or methods (e.g., utility methods like [Link]()).
•When you want to run code once when the class loads: Use a static block to initialize static
variables or to execute code once when the class is loaded into memory.
•When the nested class does not depend on the outer class: Use a static nested class when the
inner class should work independently from the outer class’s objects.
Introducing Final
The final keyword is a non-access modifier used to restrict modification.
The final keyword is used to denote constants.
It helps create constants, control inheritance and enforce fixed behavior.
The following are different contexts where the final is used:
• Variable
• Method
• Class
It applies to variables (value cannot change) methods (cannot be overridden)
and classes (cannot be extended).
It is recommended to use uppercase to declare final variables in Java. Ex : AGE
Java final Variable
In Java, we cannot change the value of a final variable. For example,
class Main {
public static void main(String[] args) {
// create a final variable
final int AGE = 32;
// try to change the final variable
AGE = 45;
[Link]("Age: " + AGE);
}
}
In the above program, we have created a final variable named age. And we have tried to change the value of
the final variable.
When we run the program, we will get a compilation error with the following message.
cannot assign a value to final variable AGE
AGE = 45;
Java final Method
In Java, the final method cannot be overridden by the child class. For example,
class FinalDemo {
// create a final method
public final void display() {
[Link]("This is a final method.");
}
}
class Main extends FinalDemo {
// try to override final method
public final void display() {
[Link]("The final method is overridden.");
}
public static void main(String[] args) {
Main obj = new Main();
[Link]();
}
}
In the above example, we have created a final method named display() inside the
FinalDemo class. Here, the Main class inherits the FinalDemo class.
We have tried to override the final method in the Main class.
When we run the program, we will get a compilation error with the following message.
display() in Main cannot override display() in FinalDemo
public final void display() {
^
overridden method is final
Java final Class
In Java, the final class cannot be inherited by another class. For example,
// create a final class
final class FinalClass {
public void display() {
[Link]("This is a final method.");
}
}
// try to extend the final class
class Main extends FinalClass {
public void display() {
[Link]("The final method is overridden.");
}
public static void main(String[] args) {
Main obj = new Main();
[Link]();
}
}
In the above example, we have created a final class named FinalClass. Here,
we have tried to inherit the final class by the Main class.
When we run the program, we will get a compilation error with the following
message.
cannot inherit from final FinalClass
class Main extends FinalClass {
^
Nested Inner Classes
• A nested class is a class defined inside another class.
Example structure:
class Outer {
class Inner {
}
}
• The scope of a nested class is bounded by the outer (enclosing) class.
• If class B is inside class A, then B cannot exist independently of A.
• A nested class can access all members of the enclosing class, including private ones.
• The enclosing class cannot access members of the nested class directly.
• A nested class defined directly inside a class becomes a member of that class.
• A nested class can also be defined inside a block (local class).
Types of Nested Classes
Static
• A static nested class belongs to the class itself, not to an object
• Can access only static members of outer class directly.
Non-static nested class (also called inner class)
• An inner class has access to all of the variables and methods of its outer class
and may refer to them directly in the same way that other non-static members of
the outer class do.
STATIC Example
class Outer {
int a = 10;
static int b = 20;
static class InnerStatic {
void show() {
// [Link](a); // ❌ cannot access
non-static member
[Link](b); // ✔ can access static
member
}
}
}
Non Static
class Outer {
int a = 10;
class Inner {
void display() {
[Link](a); // ✔ can access outer class
member directly
}
}
}
// Demonstrate an inner class.
class Outer {
int outer_x = 100;
void test() {
Inner inner = new Inner();
[Link]();
}
// this is an inner class
class Inner {
void display() {
[Link]("display: outer_x = " + outer_x);
}
}
}
class InnerClassDemo {
public static void main(String args[]) { Output:
Outer outer = new Outer(); display: outer_x = 100
[Link]();
}
}
Important Rules About Inner Class
✔ An inner class object can be created only within Outer class scope.
To create it outside, you must qualify it:
[Link] obj = [Link] Inner();
✔ Inner class can access all members of outer class.
✖ Outer class cannot access members of inner class.
class Outer {
int outer_x = 100;
void test() {
Inner inner = new Inner();
[Link]();
}
class Inner {
int y = 10; // y is local to Inner
void display() {
[Link]("display: outer_x = " + outer_x);
}
}
void showy() {
[Link](y); // ❌ ERROR: y not known here
}
}
class InnerClassDemo {
public static void main(String args[]) {
Outer outer = new Outer();
[Link]();
}
}
Here, y is declared as an instance variable of Inner. Thus, it is
not known outside of that class and it cannot be used by showy(
).
Thank
you