Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java inner class and static nested class

What is the main difference between an inner class and a static nested class in Java? Does design / implementation play a role in choosing one of these?

like image 516
Omnipotent Avatar asked Sep 16 '08 08:09

Omnipotent


People also ask

What is the difference between static nested class and Java inner class?

A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.

What is a static nested class in Java?

In Java a static nested class is essentially a normal class that has just been nested inside another class. Being static, a static nested class can only access instance variables of the enclosing class via a reference to an instance of the enclosing class.

What is the purpose of static inner class in Java?

Java static nested class A static class is a class that is created inside a class, is called a static nested class in Java. It cannot access non-static data members and methods. It can be accessed by outer class name. It can access static data members of the outer class, including private.

What is a nested class difference between static nested classes and non-static nested classes?

A non-static nested class has full access to the members of the class within which it is nested. A static nested class does not have a reference to a nesting instance, so a static nested class cannot invoke non-static methods or access non-static fields of an instance of the class within which it is nested.


1 Answers

From the Java Tutorial:

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 outerObject = new OuterClass() OuterClass.InnerClass innerObject = outerObject.new InnerClass(); 

see: Java Tutorial - Nested Classes

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.

like image 196
Martin Avatar answered Sep 21 '22 13:09

Martin