Java static inner class constructor

Is there a constructor associated with nested classes

The above code snippet compiles fine and gives output of «The value of x is 10». What i want to know here is whether a constructor is invoked when new() is used with MyInner class and MyOuter class. If yes, then is there any constructor chaining from inner class to outer class (like subclass calls constructor of super class and so on).

5 Answers 5

You can observe the constructor chain for the inner class when you extend an inner class.

public class MainClass < public MainClass(String value) < System.out.println("mainValue: " + value); >public class NestedClass < public NestedClass(String nestedValue) < System.out.println("nestedValue: " + nestedValue); >> > 

and then extend the NestedClass like this

public class NestedClassExtension extends NestedClass < public NestedClassExtension(MainClass mainClass, String nestedValue) < mainClass.super(nestedValue); >> 

so you can see that you are able to call the super constructor of your nested class passing to that constructor the MainClass , and calling .super on mainClass object instance.

Now you can create NestedClassExtension instances in this way:

NestedClassExtension extension = new NestedClassExtension(new MainClass("main"), "nested"); 

So the main class has to exist, and its constructor it is called first. Then the constructors of the nested classes.

Instead if you want create a NestedClass instance outside of the MainClass you have to write:

MainClass mc = new MainClass("main"); mc.new NestedClass("nested"); 

Another time, the MainClass has to be created first, then the nested classes.

Читайте также:  Переменные окружения python pycharm

Inner constructor is only called when you have

MyOuter.MyInner mi = mo.new MyInner(); 

otherwise it will not even invoke inner class constructor as it is not instantiated, like static blocks are executed, but instance blocks and constructors are not invoked until you create an object.

They both are instantiated using default no-arg constructor. There is no chaining like in the inheritance’s super(). It is just that you cannot instantiate an inner class without instantiation outer class first.

If you don’t specify a constructor a default constructor is created which takes no parameters. If you declare any other constructor say MyInner(int i) the creation of the default construtor is ommited and you would have to declare it yourself (if needed). Every Object (without any exceptions) is created by invoking a constructor.

If you compile your suggested code and then run the Java decompiler on it

You would see how the compiler actually declare the constructor for your inner class:

public class MyOuter$MyInner extends java.lang.Object

Here you can see that the compiler implements your inner class by declaring a final field member holding a reference to the enclosing class. The field is declared as final, and as such it is required to be provided a value to instantiate your Inner class.

When you do MyOuter.MyInner mi = mo.new MyInner() the compiler ensures the enclosing instance is passed as parameter.

This is done automatically by the compiler, and as such, you cannot chain the creation of your inner class with the creation of the outer class simply because the outer instance must already exist by the time you create the inner one.

You can, though, do constructor chaining between other declared constructors of your inner class.

For instance, if a code like this:

public class MyOuter < private int x= 10; public class MyInner < private int y = 0; public MyInner()< this(10); >public MyInner(int value) < this.y = value; >void doStuff() < System.out.println("The value of x is "+x); >> > 

Here I am chaining constructors withing the inner class.

Again, the decompiler ensures to interpret all this to make sure the outer instance is passed as a parameter to the inner one:

public class MyOuter$MyInner extends java.lang.Object

Источник

Nested Classes

The Java programming language allows you to define a class within another class. Such a class is called a nested class and is illustrated here:

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

class OuterClass < . class InnerClass < . >static class StaticNestedClass < . >>

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. As a member of the OuterClass , a nested class can be declared private , public , protected , or package private. (Recall that outer classes can only be declared public or package private.)

Why Use Nested Classes?

Compelling reasons for using nested classes include the following:

  • It is a way of logically grouping classes that are only used in one place: If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such «helper classes» makes their package more streamlined.
  • It increases encapsulation: Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private . By hiding class B within class A, A’s members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.
  • It can lead to more readable and maintainable code: Nesting small classes within top-level classes places the code closer to where it is used.

Inner Classes

As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object’s methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

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

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();

There are two special kinds of inner classes: local classes and anonymous classes.

Static Nested Classes

As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference. Inner Class and Nested Static Class Example demonstrates this.

Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience. Inner Class and Nested Static Class Example also demonstrates this.

You instantiate a static nested class the same way as a top-level class:

StaticNestedClass staticNestedObject = new StaticNestedClass();

Inner Class and Nested Static Class Example

The following example, OuterClass , along with TopLevelClass , demonstrates which class members of OuterClass an inner class ( InnerClass ), a nested static class ( StaticNestedClass ), and a top-level class ( TopLevelClass ) can access:

OuterClass.java

public class OuterClass < String outerField = "Outer field"; static String staticOuterField = "Static outer field"; class InnerClass < void accessMembers() < System.out.println(outerField); System.out.println(staticOuterField); >> static class StaticNestedClass < void accessMembers(OuterClass outer) < // Compiler error: Cannot make a static reference to the non-static // field outerField // System.out.println(outerField); System.out.println(outer.outerField); System.out.println(staticOuterField); >> public static void main(String[] args) < System.out.println("Inner class:"); System.out.println("------------"); OuterClass outerObject = new OuterClass(); OuterClass.InnerClass innerObject = outerObject.new InnerClass(); innerObject.accessMembers(); System.out.println("\nStatic nested class:"); System.out.println("--------------------"); StaticNestedClass staticNestedObject = new StaticNestedClass(); staticNestedObject.accessMembers(outerObject); System.out.println("\nTop-level class:"); System.out.println("--------------------"); TopLevelClass topLevelObject = new TopLevelClass(); topLevelObject.accessMembers(outerObject); >>

TopLevelClass.java

public class TopLevelClass < void accessMembers(OuterClass outer) < // Compiler error: Cannot make a static reference to the non-static // field OuterClass.outerField // System.out.println(OuterClass.outerField); System.out.println(outer.outerField); System.out.println(OuterClass.staticOuterField); >>

This example prints the following output:

Inner class: ------------ Outer field Static outer field Static nested class: -------------------- Outer field Static outer field Top-level class: -------------------- Outer field Static outer field

Note that a static nested class interacts with the instance members of its outer class just like any other top-level class. The static nested class StaticNestedClass can’t directly access outerField because it’s an instance variable of the enclosing class, OuterClass . The Java compiler generates an error at the highlighted statement:

static class StaticNestedClass < void accessMembers(OuterClass outer) < // Compiler error: Cannot make a static reference to the non-static // field outerField System.out.println(outerField); > >

To fix this error, access outerField through an object reference:

System.out.println(outer.outerField);

Similarly, the top-level class TopLevelClass can’t directly access outerField either.

Note: For more information about the taxonomy of the different kinds of classes in the Java programming language (which can be tricky to describe concisely, clearly, and correctly), see Joseph Darcy’s blog: Nested, Inner, Member, and Top-Level Classes.

Shadowing

If a declaration of a type (such as a member variable or a parameter name) in a particular scope (such as an inner class or a method definition) has the same name as another declaration in the enclosing scope, then the declaration shadows the declaration of the enclosing scope. You cannot refer to a shadowed declaration by its name alone. The following example, ShadowTest , demonstrates this:

public class ShadowTest < public int x = 0; class FirstLevel < public int x = 1; void methodInFirstLevel(int x) < System.out.println("x = " + x); System.out.println("this.x = " + this.x); System.out.println("ShadowTest.this.x = " + ShadowTest.this.x); >> public static void main(String. args) < ShadowTest st = new ShadowTest(); ShadowTest.FirstLevel fl = st.new FirstLevel(); fl.methodInFirstLevel(23); >>

The following is the output of this example:

x = 23 this.x = 1 ShadowTest.this.x = 0

This example defines three variables named x : the member variable of the class ShadowTest , the member variable of the inner class FirstLevel , and the parameter in the method methodInFirstLevel . The variable x defined as a parameter of the method methodInFirstLevel shadows the variable of the inner class FirstLevel . Consequently, when you use the variable x in the method methodInFirstLevel , it refers to the method parameter. To refer to the member variable of the inner class FirstLevel , use the keyword this to represent the enclosing scope:

Serialization of inner classes, including local and anonymous classes, is strongly discouraged. When the Java compiler compiles certain constructs, such as inner classes, it creates synthetic constructs; these are classes, methods, fields, and other constructs that do not have a corresponding construct in the source code. Synthetic constructs enable Java compilers to implement new Java language features without changes to the JVM. However, synthetic constructs can vary among different Java compiler implementations, which means that .class files can vary among different implementations as well. Consequently, you may have compatibility issues if you serialize an inner class and then deserialize it with a different JRE implementation. See the section Implicit and Synthetic Parameters in the section Obtaining Names of Method Parameters for more information about the synthetic constructs generated when an inner class is compiled.

Источник

Constructor Inside Inner Static Class in java?

My question is since ReaderFound is a static class how can it have an default constructor ? If so why ? Is it allowed ? If allowed what kind of constructor is this that is found inside the class Reader1$ReaderFound because it can’t be static. (Also since constructor are implicitly called to initilize an object and since ReaderFound is an static class so there would we no object of it. My point for first question)

static class means that you can instantiate this class in every context (not necessarily bound to a Reader1 ). Non-static classes are always bound to an instance of Reader1 .

@SotiriosDelimanolis What i have understood for static class is its behaves just like any top level class but is written inside some another class for some convention purpose ( Enlighten me on this if i’ve misinterpreted ) but i was really confused over static term, since for public class there was public constructor, for private there was private constructor, So what kind of default constructor this class will have ?

1 Answer 1

There are four kinds of nested classes in Java: static member classes, nonstatic member classes, anonymous classes, and local classes.

A static member class can be seen as an ordinary class that is declared inside another class. It does not need an instance of the enclosing class to be instantiated. Since no instance is required, it has no access to instance methods and variables from the enclosing class. It is able to access class members though, even if they are private.

class EnclosingClass < static class StaticMemberClass < >public static void main(String. args) < EnclosingClass.StaticMemberClass nestedInstance = new EnclosingClass.StaticMemberClass(); >> 

Being a static nested class does not mean you can’t create an instance from it. Thus, a default constructor will be created if you do not create a custom one.

Источник

Оцените статью