Static, Inner and Nested Class
Static, Inner and Nested Class
Nested Class
Presented by
Ms.G.Sakthi Priya, AP/CSE
Nested Class
• Class within another class is known as nested class
• If class B is defined within class A, then B does not exist independently
of A.
• A nested class has access to the members, including private members,
of the class in which it is nested
• The enclosing class does not have access to the members of the nested
class
• A nested class that is declared directly within its enclosing class scope is
a member of its enclosing class.
• It is also possible to declare a nested class that is local to a block.
Types of nested class
There are two types of nested classes: static and non-static.
Static nested class
• A static nested class is one that has the static modifier applied.
Because it is static, it does not have access to the non-static members
(like instance variables or methods) of the outer class directly. To
access the non-static members, it must do so through an object
instance of the outer class.
• That is, it cannot refer to non-static members of its enclosing class
directly. Because of this restriction, static nested classes are seldom
used.
Static inner class
• An inner class can also be static, which means that you can access it
without creating an object of the outer class
class Outer {
class Inner {
int y = 5; Printing of x inside mymethod is valid.
Because inner class can access outer
void mymethod(){ class members
System.out.println("I am inner class method");
System.out.println("I can access outer class members “+x);
} Printing of y inside trial method is
invalid. Because outer class can’t access
} inner class members without objects
void trial(){
System.out.println("I am outer class method");
System.out.println("outer class value "+ x); public class InnerclassDemo {
Inner i = new Inner(); public static void main(String[] args) {
System.out.println("Inner class value "+i.y); Outer o = new Outer();
i.mymethod(); o.trial();
} }
} }
Member inner class
Predict the
output
Output
display: outer_x =
Local inner class
class Outer
class InnerclassDemo{
{
public static void main(String args[])
int outer_x = 100;
{
void test()
Outer outer= new Outer();
{
outer.test();
class Inner
}
{
}
void display()
{
System.out.println("outer_x = " + outer_x);
}
}
Inner inner = new Inner(); You cannot directly create an instance of the inner class
inner.display(); from outside the outer class.
} // end of method InnerClass inner = new InnerClass(); This will result in a
compilation error if accessed outside of the outer class
}
Program Explanation
• The Inner class is defined inside the test() method of the Outer class.
• This means the Inner class is a local inner class, which can only be
instantiated and used within the test() method.
• It is not visible or accessible outside of that method, including in the
main() method or any other part of the Outer class.
Program Explanation
• Outer is a top-level class which has a variable outer_x=100 and test()
method.
• Test() method has an nested class named Inner.
• Class Inner has a display method which prints the variable outer_x of
outer class
• Instance for an Inner class is created inside test() method to call
display() method
• Inside main() method, outer class instance is created to call test()
method
• It is possible to define a nested class within the block defined by a method or
even within the body of a for loop
public class Test {
int outer_x = 100;
void test() {
for (int i = 0; i < 10; i++) {
class Inner {
void display() {
System.out.println("display: outer_x = " +
outer_x); }
}
Inner inner = new Inner();
inner.display();
}
}//end for
}//end of test method