Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

What is Inner Class in Java?

Updated on 14 November, 2022

6.9K+ views
7 min read

The concept of inner class in Java is fundamental to Java programming. In Java, a nested class and inner class are two very similar yet slightly distinct ideas of class designing and often go hand-in-hand.

A nested class is defined within another class and has two types – static nested class and non-static nested class. On the other hand, an inner class is a type of non-static nested class and is further of three types – member inner class, anonymous inner class, and local inner class.  

Check out our free courses to get an edge over the competition.

So, what do these three types of inner classes mean, and what is their use? Read on to find out in this detailed guide on Java inner class.

Meaning of Inner Class in Java

A non-static nested class or inner class is defined within another class and has access to members of the outer class (enclosing class). On the contrary, a static nested class is also a class defined within another class, but unlike a non-static nested class, it cannot access the member variables of its outer class. In the case of a non-static nested class, we are not required to create an instance of the outer class. But in the case of an inner class, we must first instantiate the outer class to be able to instantiate the inner class.

Source

Learn Online software development courses from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Types of Inner Class in Java 

In Java, inner classes are of the following three types:

  • Member inner class
  • Anonymous inner class
  • Local inner class

Check out upGrad’s Advanced Certification in DevOps

Now, let us understand each type of inner class with examples. 

1. Member Inner Class

A member inner class in Java is a non-static nested class created inside a class but outside a method. Also referred to as a regular inner class, a member inner class can be declared with access modifiers like private, public, default, and protected.

The syntax for a member inner class is as follows:

class Outer{  

 //code  

 class Inner{  

  //code  

 }  

}  

Member inner class in Java example:

Given below is a program to demonstrate how to create an inner class and access it. We will make the inner class private and use the method display_Inner()to access the class.

class DemoOuter {

   int num;

 // inner class

   private class DemoInner {

      public void print() {

         System.out.println(“This is the inner class”);

      }

   }

Check out upGrad’s Full Stack Development Bootcamp (JS/MERN) 

   // Accessing the inner class from the method within

   void display_Inner() {

      DemoInner inner = new DemoInner();

      inner.print();

   }

}

   public class MyClass {

   public static void main(String args[]) {

      // Instantiating the outer class 

      DemoOuter outer = new DemoOuter();

      

      // Accessing the display_Inner() method

      outer.display_Inner();

   }

}

Output: This is the inner class

In the above example, OuterDemo is the outer class, InnerDemo is the inner class, we are instantiating the inner class inside the method display_Inner(), which is invoked from the main method.

2. Anonymous Inner Class

An anonymous inner class in Java is an inner class declared without any class name and for which only a single object is created. Anonymous inner classes in Java are declared and instantiated at the same time. They are most valuable when we want to override the method of a class or interface. 

The syntax for an anonymous inner class in Java is as follows:

AnonymousInner an_inner = new AnonymousInner() {

   public void my_method() {

      

   //code

   }   

};

 An anonymous inner class in Java can be created in two ways:

  1. Using class
  2. Using interface

Anonymous inner class in Java example (using class):

abstract class Meal{  

  abstract void eat();  

}  

class TestAnonymousInner{  

 public static void main(String args[]){  

  Meal m=new Meal(){  

  void eat(){System.out.println(“Bon appetit!”);}  

  };  

  m.eat();  

 }  

}  

Output: Bon appetit!

Anonymous inner class in Java example (using interface):

interface Game{  

 void play();  

}  

class TestAnnonymousInner1{  

 public static void main(String args[]){  

 Game g=new Game(){  

  public void play(){System.out.println(“Outdoor games”);}  

 };  

 g.play();  

 }  

}  

Output: Outdoor games

3. Local Inner Class

A local inner class in Java is a class created inside a method. As in the case of local variables, the scope of the local inner class remains restricted within the method. 

Local inner classes are defined inside a block: a method body, an if clause, or a for loop. A local inner class is not a member of any enclosing class, but instead, it belongs to the block within which it is defined. Thus, even though a local inner class in Java cannot have any access modifiers associated with it, you can mark it abstract or final. If we want to invoke the methods of a local inner class, we have to instantiate the class inside the method.

Local inner class in Java example:

Below is an example program to demonstrate how to instantiate the object of a local inner class declared inside the method of an outer class.

package innerClass; 

public class Outer_Class 

{ 

// An instance method of class Outer_Class. 

   public void display()

  { 

// Declaration of a method local inner class. 

    class Inner_Class 

    { 

       public void msg(){ 

        System.out.println(“This is a local inner class!”); 

       } 

     } 

// Create an instance of a method local inner class and call the msg() method using object reference variable i. 

     Inner_Class i = new Inner_Class(); 

       i.msg(); 

  } 

public static void main(String[] args) 

{ 

// Create an object of the outer class Outer_Class. 

     Outer_Class o = new Outer_Class(); 

       o.display(); 

   } 

}

Output: This is a local inner class!

With the fundamentals of Java inner class in mind, let us understand why we need them in the first place.

Advantages of Java Inner Class

An inner class in Java brings three significant utilities to the table. These are as follows:

1. Since a Java inner class can access the data members and methods of the outer/main class, including private, it represents a specific type of relationship.

2. A Java inner class significantly reduces coding since it requires less to write.

3. An inner class in Java logically groups classes and interfaces in a single place, thereby enhancing the readability of the code and making it easier to maintain.

Summing Up

The concept of nested classes simplifies and adds flexibility to creating classes anywhere in Java programming. In this regard, a non-static nested class known as inner class is particularly useful, as we have already seen in our discussion so far. 

We would like to wrap up by stating two instances where the use of inner class seems most appropriate. First, a Java inner class is ideal when two classes have a close relationship and are not accessible by any other class. And second, inner classes are commonplace in Java GUI programming event handling. Therefore, Java inner classes can be safely used where listeners and their GUI components are involved.

Wondering where to learn Java?

upGrad’s Job-linked PG Certification in Software Engineering is what you’re looking for!

Specially designed for fresh graduates and final years, upGrad’s Job-linked PG Certification in Software Engineering is perfect for those who want to learn to program and get placed in entry-level software roles. This 5-month online program will teach top software skills like Java, JavaScript, HTML5, DSA, AWS, MERN, and more!

Program Highlights:

  • Specialisation in MERN Stack/Cloud-Native
  • 500+ hours of online learning
  • 350+ hours of hands-on training
  • 50+ live sessions
  • Five industry projects
  • Networking with industry experts
  • Q&A forums with peers and industry experts
  • Learning support from teaching assistants and industry mentors

Sign up today to learn from the best in the industry!

Frequently Asked Questions (FAQs)

1. What is the difference between inner class and subclass?

An inner class is a class that is nested or defined within another class. On the other hand, a subclass is a class that is derived from another class. Moreover, while an inner class can only be accessed by using the reference of the outer class within which it is nested, a subclass can be accessed directly unless the subclass has inherited private variables.

2. Can we write class inside a class in Java?

Yes, we can define a class within another class in Java. Such a class is known as a nested class, and it allows us to logically organise classes in one place, thereby creating a more maintainable and readable code. As part of its enclosing class, a nested class can be declared public, private, package-private, or protected.

3. Can Java inner class be private?

Yes, a Java inner class can be private. But once an inner class is declared private, it cannot be accessed from an object that’s outside the class. Inner and outer classes have access to each other’s private instance variables and private methods. As long as we are within the outer or inner class, the modifiers private and public have the same effect.

RELATED PROGRAMS