Java Inner Classes

JK1708 
Created at Jul 19, 2023 07:18:22
Updated at May 10, 2024 14:01:53 
  6,636   0   0  

In Java, it is also possible to nest classes (a class within a class). The purpose of nested classes is to group classes that belong together, which makes your code more readable and maintainable.

To access the inner class, create an object of the outer class, and then create an object of the inner class:

class OuterClass {
  int x = 10;

  class InnerClass {
    int y = 5;
  }
}

public class Main {
  public static void main(String[] args) {
    OuterClass myOuter = new OuterClass();
    OuterClass.InnerClass myInner = myOuter.new InnerClass();
    System.out.println(myInner.y + myOuter.x);
  }
}

// Outputs 15 (5 + 10)

Private Inner Class

Unlike a "regular" class, an inner class can be private or protected. If you don't want outside objects to access the inner class, declare the class as private:

class OuterClass {
  int x = 10;

  private class InnerClass {
    int y = 5;
  }
}

public class Main {
  public static void main(String[] args) {
    OuterClass myOuter = new OuterClass();
    OuterClass.InnerClass myInner = myOuter.new InnerClass();
    System.out.println(myInner.y + myOuter.x);
  }
}

If you try to access a private inner class from an outside class, an error occurs:

Main.java:13: error: OuterClass.InnerClass has private access in OuterClass
    OuterClass.InnerClass myInner = myOuter.new InnerClass();
              ^

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 OuterClass {
  int x = 10;

  static class InnerClass {
    int y = 5;
  }
}

public class Main {
  public static void main(String[] args) {
    OuterClass.InnerClass myInner = new OuterClass.InnerClass();
    System.out.println(myInner.y);
  }
}

// Outputs 5

Access Outer Class From Inner Class

One advantage of inner classes, is that they can access attributes and methods of the outer class:

class OuterClass {
  int x = 10;

  class InnerClass {
    public int myInnerMethod() {
      return x;
    }
  }
}

public class Main {
  public static void main(String[] args) {
    OuterClass myOuter = new OuterClass();
    OuterClass.InnerClass myInner = myOuter.new InnerClass();
    System.out.println(myInner.myInnerMethod());
  }
}

// Outputs 10

 



Tags: Access Outer Class From Inner Class Java Java Inner Classes Private Inner Class Static Inner Class Share on Facebook Share on X

◀ PREVIOUS
Java Polymorphism

▶ NEXT
Java Tutorials associated with AP Computer Science A

  Comments 0
SIMILAR POSTS

Java Polymorphism

(created at Jul 18, 2023)

Java Inheritance (Subclass and Superclass)

(updated at May 10, 2024)

Java Packages

(updated at May 10, 2024)

Java Classes and Objects

(updated at May 10, 2024)

Java Recursion

(updated at May 10, 2024)

Java Scope

(updated at May 10, 2024)

Java Methods

(updated at May 10, 2024)

Java While Loop/Do While Loop/For Loop/For-Each Loop/Break/Continue

(updated at May 13, 2024)

Java Switch Statements

(updated at May 15, 2024)

Java Short Hand If...Else (Ternary Operator)

(updated at May 15, 2024)

Java If ... Else

(updated at May 15, 2024)

Java Math

(updated at May 15, 2024)

Java Variables

(updated at May 15, 2024)

Java Comments

(updated at May 15, 2024)

The Print() Method

(updated at May 10, 2024)

Java Syntax

(updated at May 15, 2024)

Java Getting Started

(updated at May 15, 2024)

What is Java?

(created at Jul 07, 2023)

Machine Learning Types and Programming Languages

(updated at Nov 29, 2023)

Java Tutorials associated with AP Computer Science A

(updated at Jun 15, 2024)

Creating a simple Java Servlet (Web Server Page) with Apache Maven on Microsoft Windows

(created at Jan 28, 2024)

Java Servlet Example

(created at Feb 19, 2024)

Difference between Java and Javascript

(updated at Oct 03, 2024)

OTHER POSTS IN THE SAME CATEGORY

Java Servlet Example

(created at Feb 19, 2024)

How do I replace content that based on the HTML UI Template

(created at Feb 12, 2024)

Creating a simple Java Servlet (Web Server Page) with Apache Maven on Microsoft Windows

(created at Jan 28, 2024)

Dataset of California Foodbanks

(updated at Feb 12, 2024)

Java Tutorials associated with AP Computer Science A

(updated at Jun 15, 2024)

Java Polymorphism

(created at Jul 18, 2023)

Java Inheritance (Subclass and Superclass)

(updated at May 10, 2024)

Java Packages

(updated at May 10, 2024)

Java Abstract Classes and Methods

(updated at Jan 02, 2024)

Java Classes and Objects

(updated at May 10, 2024)

Java Recursion

(updated at May 10, 2024)

Java Scope

(updated at May 10, 2024)

Java Methods

(updated at May 10, 2024)

Java Arrays

(updated at May 13, 2024)

Java While Loop/Do While Loop/For Loop/For-Each Loop/Break/Continue

(updated at May 13, 2024)

Java Switch Statements

(updated at May 15, 2024)

Java Short Hand If...Else (Ternary Operator)

(updated at May 15, 2024)

Java If ... Else

(updated at May 15, 2024)

Java Math

(updated at May 15, 2024)

Java Variables

(updated at May 15, 2024)

UPDATES

Why Outlook’s Redirection Option Is a Game-Changer

(created at Aug 24, 2025)

Where to Eat with Your i-Card at UIUC and How to Track Your Dining Dollars

(updated at Aug 24, 2025)

Why Every Freshman Needs the Illinois App at UIUC

(updated at Aug 24, 2025)

Did you get Selective Service System(SSS) Form 3C?

(updated at Aug 17, 2025)

How to Receive Mail and Packages in University Housing at UIUC

(updated at Aug 11, 2025)

BlackPink's refreshing song - Jump

(updated at Aug 08, 2025)

Poisonous Mushrooms sprouted along the roadside after Typhoon

(updated at Aug 06, 2025)

Annual Weather Forecasting in Illinois based on Month

(updated at Aug 06, 2025)

My name has a typo in MyIllini - Need a Biographical change form

(updated at Jul 31, 2025)

Free Transportation Systems for UIUC students, faculty, and staff with I-Card

(updated at Jul 31, 2025)

What you can do with I-Card at UIUC

(updated at Jul 31, 2025)

Public Transportation between Chicago O'Hare International Airport and UIUC (University of Illinois at Urbana-Champaign)

(updated at Jul 31, 2025)

Selecting a Bed Configuration before you move-in at UIUC Dormitory

(updated at Jul 30, 2025)

Student Health Insurance Waiver: Major Deadlines You Can’t Miss

(updated at Jul 22, 2025)

Types of Memory and Storage

(updated at Jul 22, 2025)

Sample Curriculum Comparison CS versus CS+GGIS at UIUC

(updated at May 31, 2025)

UIUC 2025-2026 Academic Calendar

(updated at May 26, 2025)

IU (아이유) appeared at Mask Singer with Violet Fragrance (보라빛 향기)

(updated at Apr 17, 2025)

What is Model Context Protocol (MCP)? How to build AI Agents?

(updated at Apr 17, 2025)

송소희(Song Sohee) - Not a Dream

(updated at Apr 08, 2025)

DOH KYUNG SOO & LEE SUHYUN - Rewrite The Stars cover

(created at Apr 08, 2025)

😲😭 디오(D.O.) - 후라이의 꿈 + Rewrite the stars(with. 수현)

(created at Apr 08, 2025)

D.O. (도경수) & IU (아이유) - Love Wins All | IU’s Palette (아이유의 팔레트)

(updated at Apr 08, 2025)

Lie - Legend song by BIGBANG

(updated at Feb 26, 2025)

Happy New Year Message with Mathematical Equations

(updated at Jan 02, 2025)

Life Quotes from Google CTO Will Grannis emphasizes the importance of data and the problem definition

(updated at Dec 17, 2024)

Life Quotes from Netflix CTO Elizabeth Stone in 2023

(updated at Dec 17, 2024)

Exploring UC Irvine (aka UCI) - School and its Majors

(updated at Dec 13, 2024)

Understanding Rose-Hulman Institute of Technology

(updated at Dec 13, 2024)

Chilling Acrobatic Taekwondo! The Birth of a Poomsae Prodigy - Byeon Jae-yeong Wins 1st Place at the Hong Kong World Poomsae Championships

(created at Dec 12, 2024)

IU's breathtakingly beautiful "eight" live performance, captivating the hearts of the audience with her dazzling vocals

(created at Dec 10, 2024)

Navigation for UMass Amherst (aka University of Massachusetts Amherst) - Campus Life and Underground Majors

(updated at Dec 10, 2024)

Exploring UC San Diego (aka UCSD) - School and its Majors

(updated at Dec 10, 2024)

How to access websites blocked by ESNI and ECH settings with Firefox!

(updated at Nov 29, 2024)

[#2024MAMA] G-DRAGON - HOME SWEET HOME (feat. Taeyang, Daesung) | Mnet 241123

(updated at Nov 27, 2024)

Eveything you tell HR is confidential

(updated at Nov 27, 2024)

The hippie perm of NewJeans' Danielle 

(updated at Nov 23, 2024)

LoL Worldcup - Worlds 2024 Finals Opening Ceremony Presented by Mastercard ft. Linkin Park, Ashnikko and More!

(created at Nov 18, 2024)

Danielle was featured on the UK Fashion Pop Magazine cover

(updated at Nov 15, 2024)

IU Photos from her family trip

(updated at Nov 09, 2024)

Men vs. Women Taekwondo Sparring - Beautiful Taekwondo Star Tammy's Dazzling Roundhouse Kicks

(updated at Nov 09, 2024)

Legendary Taekwondo Match of the Korean National Sports Festival in High School Division

(updated at Nov 09, 2024)

Legendary Taekwondo 540 degree Kick - Champion Hyun-goo Noh

(created at Nov 09, 2024)

The difference between Equation and Formula

(created at Nov 08, 2024)

Lengendary Turkish Taekwondo player Tazegul at 2015 WTF World Taekwondo Championships

(updated at Nov 08, 2024)

World Rank #2 - Turkey TKD Legend Servet Tazegül

(created at Nov 07, 2024)

Irvine Restaurant American-Style Vietnamese Food Brodard (ft. Ultimate Spring Roll)

(updated at Nov 03, 2024)

Block unwanted URLs for comfortable web browsing with Chrome Addon - URL Blocker

(updated at Nov 01, 2024)

The Gigant Cowboys of Virginia City, Nevada 1889 - AI Generated Photos

(updated at Oct 28, 2024)

Sushi Koto: The "Ohtani" Sushi Spot in Irvine

(created at Oct 26, 2024)