Learn nested classes in java

Hi!! Welcome to flower brackets blog. In this post we will learn nested classes in java.

Nested class or inner is a class within another class.

Syntax:

class A  // outer class
{
   ......
   class B   // inner class 
   {
      .....
   } 
}

NOTE:

  1. Nested class or inner class is a member of its enclosing class. Hence scope of nested class is constrained by its Outer class.
  2. Access modifiers for nested class are public, protected, private or package private.
  3. package private is the default access modifier for nested class.
  4. Outer class does not have access to the members of nested class. While nested class has access to members, also private members of Outer class.
  5. Types of nested class – member static nested class and non – static nested class.

Static nested classes

  • Static nested class is a member of outer class.
  • Outer class can have static members. But when an inner class is declared with keyword “static” then this class will become static member to outer class.
  • Like static members we need not instantiate outer class to call static members of static nested inner class.
  • Static nested class cannot access instance variable of outer class.

Syntax:

class OuterClass
{
   static class InnerClass 
   {

   } 
}

Here’s a java program on static nested class,

// outer class
public class
ClassOuter
{
// static nested class
static class Nested
{
public void print()
{
System.out.println("static nested class");
}
}
}

public class NestedDemo
{
public static void main(String[] args)
{
ClassOuter.Nested obj = new ClassOuter.Nested();
obj.print();
}
}

Output:

static nested class


Non static nested class or inner class

Inner class which is declared without “static” keyword is non-static nested class.

A non-static inner class can be declared only with non-static members. Methods in non-static inner class can access all the members (static and non-static) without reference.

All the static and non-static members of outer class can be accessed by methods of non-static inner class directly.

Syntax:

class OuterClass
{
   ......
   class InnerClass 
   {
      .....
   } 
}

Let’s see a program,

// outer class
class Outer
{
int number = 100;
static int even = 4;
// inner class
class Inner
{
void print()
{
System.out.println("calling print() method of inner class");
System.out.println(number);
System.out.println(even);
}
}
}

public class NonStaticNestedDemo
{
public static void main(String[] args)
{
Outer obj1 = new Outer();
Outer.Inner obj2 = obj1.new Inner();
obj2.print();
}
}

Output:

calling print() method of inner class
100
4


Also read – learn static keyword in java


Local inner class

A class defined inside a method of outer class is called as local inner class or method local inner class. For example,

// outer class
public class Outer
{
double number = 566.56;
void Display()
{
// local inner class
class Inner
{
void Print()
{
System.out.println("this is local inner class");
System.out.println(number);
}
}// end local inner class
// instantiating local inner class
Inner obj = new Inner();
obj.Print();
}
public static void main(String[] args)
{
Outer obj1 = new Outer();
obj1.Display();
}
}

Output:

this is local inner class
566.56


Anonymous inner class

A class without definition and name inside a method is anonymous inner class. For example,

interface Anonymous
{
void print();
}
// outer class
public class Outer
{
public static void main(String[] args)
{
// anonymous inner class
Anonymous obj = new Anonymous()
{
@Override
public void print()
{
System.out.println("this is anonymous inner class");
}
}; // end of anonymous inner class
obj.print();
}
}

Output:

this is anonymous inner class

NOTE : Anonymous inner class is used to provide definition for interfaces.

Related Posts