What are inner classes and static nested classes

+1 vote
What is the difference between inner classes and static nested classes?
May 3, 2018 in Java by anonymous
4,024 views

2 answers to this question.

0 votes

These are some major differences and similarities between Java inner class and static nested class.

Inner class:

  • Access outer class both instance and static methods and fields
  • Associated with instance of enclosing class so to instantiate it needs an instance of outer class

    Outerclass.InnerClass innerClass = outerClass.new Innerclass();
  • Cannot define any static members itself

  • Cannot have Class or Interface declaration

Static nested class

  • Cannot access outer class instance methods or fields

  • Not associated with any instance of enclosing class, we can instantiate it:

    OuterClass.StaticNestedClass nestObject = new OuterClass.StaticNestedClass();

Similarities

  • Both Inner classes can access even private fields and methods of outer class
  • Also the Outer class have access to private fields and methods of inner classes
  • Both classes can have private, protected or public access modifier
answered May 3, 2018 by sharth
• 3,370 points
0 votes

Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are simply called static nested classes. Non-static nested classes are called inner classes.

Static nested classes are accessed using the enclosing class name:

OuterClass.StaticNestedClass

For example, to create an object for the static nested class, use this syntax:

OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();

Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:

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

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

For completeness note that there is also such a thing as an inner class without an enclosing instance:

class A {
  int t() { return 1; }
  static A a =  new A() { int t() { return 2; } };
}

Here, new A() { ... } is an inner class defined in a static context and does not have an enclosing instance.

answered Dec 3, 2018 by Sushmita
• 6,920 points

Related Questions In Java

0 votes
1 answer

What is the difference between static and inner class?

An inner class, cannot be static, so ...READ MORE

answered Jul 11, 2018 in Java by sophia
• 1,400 points
1,229 views
0 votes
0 answers

Java inner class and static nested class

What is the main difference between an ...READ MORE

Apr 21, 2022 in Java by Rahul
• 3,380 points
381 views
0 votes
1 answer

What issues should be considered when we are overriding equals and hashCode in Java?

equals() must define an equivalent relation and ...READ MORE

answered Jun 21, 2018 in Java by Sushmita
• 6,920 points
735 views
0 votes
1 answer

Interface variable are static and final by default. Why?

Interface variables are static because Java interfaces ...READ MORE

answered Jul 4, 2018 in Java by Daisy
• 8,140 points
13,467 views
0 votes
1 answer

Java Static nested class

Hi, to understand their usage, you must ...READ MORE

answered Jun 8, 2018 in Java by v.liyyah
• 1,300 points
703 views
0 votes
1 answer

Static Classes In Java

Java has static nested classes but it sounds like ...READ MORE

answered Dec 23, 2020 in Java by Gitika
• 65,770 points

edited Jul 5, 2023 by Khan Sarfaraz 924 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 81,243 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
8,365 views
0 votes
1 answer

what are the ways in which a list can be iterated

  There are 5 ways to iterate over ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,370 points
1,106 views
0 votes
1 answer

What is the difference between Runnable and extends Thread

Runnable is the preferred way to create ...READ MORE

answered May 1, 2018 in Java by sharth
• 3,370 points
1,160 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP