0% found this document useful (0 votes)
72 views

14 Inner Classes, Garbage Collector and Wrapper Classes 04 Aug 2020material - I - 04 Aug 2020 - Lecture8.1 Java - Innerclasses

Java allows classes to be defined within other classes. There are several types of nested classes in Java including inner classes, anonymous classes, local classes, and static nested classes. Inner classes can access all members of the outer class including private variables and methods. They make code more readable and maintainable by grouping related classes together. Static nested classes cannot access non-static members of the outer class but don't require an outer class object to be instantiated.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

14 Inner Classes, Garbage Collector and Wrapper Classes 04 Aug 2020material - I - 04 Aug 2020 - Lecture8.1 Java - Innerclasses

Java allows classes to be defined within other classes. There are several types of nested classes in Java including inner classes, anonymous classes, local classes, and static nested classes. Inner classes can access all members of the outer class including private variables and methods. They make code more readable and maintainable by grouping related classes together. Static nested classes cannot access non-static members of the outer class but don't require an outer class object to be instantiated.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Inner Classes

Java inner class or nested class is a class which is declared inside the class or
interface.

We use inner classes to logically group classes and interfaces in one place so that it can
be more readable and maintainable.

Additionally, it can access all the members of outer class including private data members
and methods.

Syntax of Inner class

1. class Java_Outer_class{  
2.  //code  
3.  class Java_Inner_class{  
4.   //code  
5.  }  
6. }  

Advantage of java inner classes


There are basically three advantages of inner classes in java. They are as follows:

1) Nested classes represent a special type of relationship that is it can access all the
members (data members and methods) of outer class including private.

2) Nested classes are used to develop more readable and maintainable


code because it logically group classes and interfaces in one place only.

3) Code Optimization: It requires less code to write.

Do You Know
o What is the internal code generated by the compiler for member inner class ?
o What are the two ways to create annonymous inner class ?
o Can we access the non-final local variable inside the local inner class ?
o How to access the static nested class ?
o Can we define an interface within the class ?
o Can we define a class within the interface ?

Difference between nested class and inner class in


Java
Inner class is a part of nested class. Non-static nested classes are known as inner
classes.
Types of Nested classes
There are two types of nested classes non-static and static nested classes.The non-static
nested classes are also known as inner classes.

o Non-static nested class (inner class)


1. Member inner class
2. Anonymous inner class
3. Local inner class
o Static nested class

Type Description

Member Inner Class A class created within class and outside method.

Anonymous Inner A class created for implementing interface or extending class. Its name is decided by t
Class compiler.

Local Inner Class A class created within method.

Static Nested Class A static class created within class.

Nested Interface An interface created within class or interface.

Java Member inner class


A non-static class that is created inside a class but outside a method is called member
inner class.

Syntax:

1. class Outer{  
2.  //code  
3.  class Inner{  
4.   //code  
5.  }  
6. }  
Java Member inner class example
In this example, we are creating msg() method in member inner class that is accessing
the private data member of outer class.

1. class TestMemberOuter1{  
 private int data=30;  
 class Inner{  
  void msg(){System.out.println("data is "+data);}  
 }  
 public static void main(String args[]){  
  TestMemberOuter1 obj=new TestMemberOuter1();  
  TestMemberOuter1.Inner in=obj.new Inner();  
  in.msg();  
 }  
}  
Test it Now

Output:

data is 30

Java Anonymous inner class


A class that have no name is known as anonymous inner class in java. It should be used
if you have to override method of class or interface. Java Anonymous inner class can be
created by two ways:

1. Class (may be abstract or concrete).


2. Interface

Java anonymous inner class example using class


1. abstract class Person{  
2.   abstract void eat();  
3. }  
4. class TestAnonymousInner{  
5.  public static void main(String args[]){  
6.   Person p=new Person(){  
7.   void eat(){System.out.println("nice fruits");}  
8.   };  
9.   p.eat();  
10.  }  
11. }  
Test it Now
Output:

nice fruits

abstract class Fun


{
abstract void eat();
}
class Don
{
public static void main(String[] args)
{
Fun s1=new Fun()// no semi colon
{
void eat()
{
System.out.println("harsha");
}
};
s1.eat();
}
}

Java Local inner class


A class i.e. created inside a method is called local inner class in java. If you want to
invoke the methods of local inner class, you must instantiate this class inside the
method.

Java local inner class example


public class localInner1{  
 private int data=30;//instance variable  
 void display(){  
  class Local{  
   void msg(){System.out.println(data);}  
  }  
  Local l=new Local();  
  l.msg();  
 }  
 public static void main(String args[]){  
  localInner1 obj=new localInner1();  
  obj.display();  
 }  
}  
Test it Now

Output:
30

class Don
{
private int a=10;
void man()
{
class Fun
{
void msg()
{
System.out.println(a);
}

}
Fun l=new Fun();
l.msg();

}
public static void main(String[] args)
{

Don d1=new Don();

d1.man();

}
}

Java static nested class


A static class i.e. created inside a class is called static nested class in java. It cannot
access non-static data members and methods. It can be accessed by outer class name.

o It can access static data members of outer class including private.


o Static nested class cannot access non-static (instance) data member or method.

Java static nested class example with instance


method
1. class TestOuter1{  
2.   static int data=30;  
3.   static class Inner{  
4.    void msg(){System.out.println("data is "+data);}  
5.   }  
6.   public static void main(String args[]){  
7.   TestOuter1.Inner obj=new TestOuter1.Inner();  
8.   obj.msg();  
9.   }  
10. }  
Test it Now
Output:

data is 30

In this example, you need to create the instance of static nested class because it has
instance method msg(). But you don't need to create the object of Outer class because
nested class is static and static properties, methods or classes can be accessed without
object.

Java static nested class example with static method


If you have the static member inside static nested class, you don't need to create
instance of static nested class.

1. class TestOuter2{  
2.   static int data=30;  
3.   static class Inner{  
4.    static void msg(){System.out.println("data is "+data);}  
5.   }  
6.   public static void main(String args[]){  
7.   TestOuter2.Inner.msg();//no need to create the instance of static nested class  
8.   }  
9. }  
Test it Now

Output:

data is 30

class Don
{
static int a=10;
static class Van
{
static void msg()
{
System.out.println("hello");
System.out.println(a);
}
}
public static void main(String[] args)
{
Van.msg();
}
}

You might also like