Instantiating inner class
I think you want to declare the HashPerson
class as static
. Otherwise it can only be instantiated in the context of the containing class, either in a method of the containing class or using code like this:
ContainingClass container = new ContainingClass();
HashPerson william = container.new HashPerson("willy");
Actually, my rule-of-thumb is to make any nested class static, unless I have a special reason not to. This is also more efficient, because non-static nested classes (called inner classes) always contain an implicit reference to the containing object.
Instantiating inner class of class
An inner class is a class declared inside another one without using the static
keyword. An instance of an inner class has to belong to an instance of the outer class. For example, a good example of an inner class might be
public class School {
public class Pupil {
}
}
Every Pupil
has to belong to a School
so you cannot construct a Pupil
instance without a school. You can do
School school = new School();
School.Pupil pupil = school.new Pupil();
The second line creates a new Pupil
belonging to a particular School
.
Inside the Pupil
class you get the school the pupil belongs to by doing School.this
. You can use fields of the School
class by doing School.this.someField
.
If you want to be able to create Foo2
instances without an instance of Foo
you can write
public static class Foo2
instead. In most cases, you probably should use static nested classes in preference to inner classes.
Java Nested Class: Instantiating inner class from outer class
While in the first example, the instance inner
has to be declared static
to be used in another static context.
In the latter, the global variable inner
is left unused as the local declaration and initialization takes priority.
Why can't main function instantiate inner class?
From Java Tutorials:
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 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.
So if you want to be able to instantiate an inner class, you need to have an instance of the outer class. In instance methods you don't need it because you're always referring to 'this'
How instantiating an inner class in Java really works?
This is the name of the class, and it's likely named this way to make it easier for the compiler to find the definition.
If you declare your variable as just being of type InnerClass
, it will look for the file InnerClass.java
, but there is no such file.
The dot notation indicates that it's actually a member of OuterClass
, so it will look for the definition within the file OuterClass.java
.
This is the same thing as using a class from a library,
com.example.MyLibrary.ExternalClass myExternalObject;
How to instantiate an inner class with reflection in Java?
There's an extra "hidden" parameter, which is the instance of the enclosing class. You'll need to get at the constructor using Class.getDeclaredConstructor
and then supply an instance of the enclosing class as an argument. For example:
// All exception handling omitted!
Class<?> enclosingClass = Class.forName("com.mycompany.Mother");
Object enclosingInstance = enclosingClass.newInstance();
Class<?> innerClass = Class.forName("com.mycompany.Mother$Child");
Constructor<?> ctor = innerClass.getDeclaredConstructor(enclosingClass);
Object innerInstance = ctor.newInstance(enclosingInstance);
Alternatively, if the nested class doesn't actually need to refer to an enclosing instance, make it a nested static class instead:
public class Mother {
public static class Child {
public void doStuff() {
// ...
}
}
}
Related Topics
What Is the Purpose of @Namedarg Annotation in Javafx 8
Jdk Dateformatter Parsing Dayofweek in German Locale, Java8 VS Java9
Differencebetween I++ & ++I in a for Loop
How to Convert Ascii Code (0-255) to Its Corresponding Character
Why Are the Rsa-Sha256 Signatures I Generate with Openssl and Java Different
Hiding Instance Variables of a Class
Installed Java 7 on MAC Os X But Terminal Is Still Using Version 6
Debug a Java Application Without Starting the Jvm with Debug Arguments
Why Does Parallel Stream with Lambda in Static Initializer Cause a Deadlock
Has Been Compiled by a More Recent Version of the Java Runtime (Class File Version 57.0)
How to Compile & Run Java Program in Another Java Program
Why Shouldn't I Call Setvisible(True) Before Adding Components
Bytebuffer.Allocate() VS. Bytebuffer.Allocatedirect()
Is Httpsession Thread Safe, Are Set/Get Attribute Thread Safe Operations
What Does It Mean to Program to an Interface
The Connection Between 'System.Out.Println()' and 'Tostring()' in Java