In Java, an inner class is defined within the body of another class. Inner classes can access the members of the outer class, including private ones, and are used to logically group classes that are only used in one place.
public class OuterClass {
private String message = "Hello from OuterClass";
class InnerClass {
public void display() {
System.out.println(message);
}
}
}
Console Output:
Hello from OuterClass
A nested inner class is a class declared inside another class. It can be static or non-static.
public class OuterClass {
static class StaticNestedClass {
public void display() {
System.out.println("Inside Static Nested Class");
}
}
}
Console Output:
Inside Static Nested Class
An anonymous inner class is a class without a name and is instantiated in place. It's often used in graphical user interface (GUI) applications.
abstract class Animal {
abstract void sound();
}
public class Main {
public static void main(String[] args) {
Animal dog = new Animal() {
void sound() {
System.out.println("Bark");
}
};
dog.sound();
}
}
Console Output:
Bark
Local inner classes are defined within a method. They have access to the method's local variables that are declared final.
public class OuterClass {
void display() {
class LocalInner {
void msg() {
System.out.println("Inside Local Inner Class");
}
}
LocalInner local = new LocalInner();
local.msg();
}
}
Console Output:
Inside Local Inner Class
Member inner classes are defined at the member level of the outer class. They can access all members of the outer class, including private ones.
public class OuterClass {
private String msg = "Hello from Member Inner Class";
class MemberInner {
void display() {
System.out.println(msg);
}
}
}
Console Output:
Hello from Member Inner Class
Static nested classes are similar to static members. They can access static data members of the outer class but cannot access non-static data members and methods.
public class OuterClass {
static int data = 30;
static class StaticNested {
void msg() {
System.out.println("Data: " + data);
}
}
}
Console Output:
Data: 30
To access an inner class, you need to create an instance of the outer class first. Then, create an instance of the inner class using the outer class instance.
public class OuterClass {
class Inner {
void msg() {
System.out.println("Accessing Inner Class");
}
}
public static void main(String[] args) {
OuterClass outer = new OuterClass();
OuterClass.Inner inner = outer.new Inner();
inner.msg();
}
}
Console Output:
Accessing Inner Class
Inner classes can make code more readable and maintainable by logically grouping classes together. They also have access to the outer class's private members.
// Example showing advantage of inner class
public class OuterClass {
private String data = "Inner class access";
class Inner {
void display() {
System.out.println(data);
}
}
}
Console Output:
Inner class access
Inner classes can make code more complex and harder to understand if overused. They also increase the size of the outer class and can lead to memory leaks if not handled properly.
// Example illustrating potential complexity
public class OuterClass {
class Inner {
void complexMethod() {
System.out.println("Complexity can increase");
}
}
}
Console Output:
Complexity can increase
Inner classes are useful in scenarios where a class is needed only in one place. They are commonly used in event handling and callback mechanisms.
// Example of inner class in event handling
public class Button {
void click() {
class ClickListener {
void onClick() {
System.out.println("Button clicked");
}
}
ClickListener listener = new ClickListener();
listener.onClick();
}
}
Console Output:
Button clicked
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies