0% found this document useful (0 votes)
17 views21 pages

Static, Inner and Nested Class

Uploaded by

arunanirmal2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views21 pages

Static, Inner and Nested Class

Uploaded by

arunanirmal2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Static, Inner and

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 {

static class Inner {


int y = 5;
}
}

public class Demo {


public static void main(String[] args) {
Outer.Inner myIn = new Outer.Inner();
System.out.println(myIn.y); // Output 5
}
}
Program Explanation
• Outer is a top-level class.
• Inner is a nested static class within the Outer class.
• In the Demo class's main method, new instance of the Inner class is
created.
Outer.Inner myIn = new Outer.Inner();
• System.out.println(myIn.y); prints the value of the y.
• Since Inner is a static nested class, you don't need an instance of the
Outer class
Class Poll
class Outer{
static class NestedClass {
void display() {
System.out.println("This is a static nested class.");
}
}
}
Outer.NestedClass obj = new Outer.NestedClass();
obj.display();
Non static nested class / Inner class
• The most important type of nested class is the inner class. An inner class is a
non-static nested class
• Inner class means one class which is a member of another class.
• 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.
• Outer class cannot access variables and methods of inner class directly
• The outer class can access the variables and methods of the inner class, but it
needs to do so through an instance of the inner class.
Syntax of inner class
class Java_Outer_class{
//code
class Java_Inner_class{
//code
}
}
Types of inner class
Member inner class
• A class created within class and outside method

Anonymous inner class


• A class created for implementing interface or
extending class. Its name is decided by the java
compiler

Local inner class


• A class created within method
Example 1
class Outer {
int x = 10; Member Inner Class
class Inner {
int y = 5; 5
10
}
}
public class InnerclassDemo {
public static void main(String[] args) {
Outer o = new Outer();
Outer.Inner i= o.new Inner(); // Creating an instance of InnerClass using the outer class instance
System.out.println(i.y); You cannot directly create an instance of the inner class
System.out.println(o.x); from outside the outer class.
InnerClass inner = new InnerClass(); This will
} result in a compilation error if accessed outside of
Program Explanation
• Outer is a top-level class which has a variable x=10
• Inner is a nested class within Outer Class which has a variable y=10
• In the main method of the InnerclassDemo class,
• Instance of the Outer class is created using the statement,
Outer o = new Outer();
• Instance of the Inner class is created using outer class instance.
Outer.Inner i = o.new Inner();
• System.out.println(i.y); prints the value of the y of the i instance, which is 5.
• System.out.println(o.x); prints the value of the x of the o instance, which is 10.
Example 2
class Outer{ Member Inner Class
int x = 10;

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

public static void main(String args[]) {


Test outer = new Main();
outer.test();
}
}
Without /* Myclass implement the methods of Age
Interface*/
Anonymous class MyClass implements Age
{
Inner class @Override
public void getAge()
interface Age {
{ // printing the age
System.out.print("Age is "+x);
int x = 21; }
}class Demo
void getAge(); {
} public static void main(String[] args)
{
// Myclass is implementation class of Age
interface
MyClass obj=new MyClass();
// calling getage() method implemented at
Myclass
obj.getAge();
}
}
Using class Demo
{
Anonymous public static void main(String[] args) {
Inner class // Myclass is hidden inner class of Age
interface Age interface
// whose name is not written but an object to
{ it
int x = 21; // is created.
Age oj1 = new Age() {
void getAge(); @Override
public void getAge() {
} // printing age
System.out.print("Age is "+x);
}
};
oj1.getAge();
} Output:
} Age is 21
Program Explanation
• An anonymous class is created that implements the Age interface. This
anonymous class provides an implementation for the getAge method.
• In the getAge method of the anonymous class, it prints the value of the
constant x, which is defined in the Age interface. The value of x is 21.
• An instance of the anonymous class is created and stored in the
reference variable oj1.
• Finally, the getAge method of the anonymous class is called using
oj1.getAge();
• When you run the Demo class, it will create an instance of the anonymous
class and call the getAge() method which provides the output “Age is 21”

You might also like