This question already has answers here:
Java: Non-static nested classes and instance.super()
(5 answers)
Closed 6 years ago.
class OuterClass
{
static class InnerClassOne
{
//Class as a static member
}
class InnerClassTwo
{
//Class as a non-static member
}
}
class AnotherClassOne extends OuterClass.InnerClassOne
{
}
class AnotherClassTwo extends OuterClass.InnerClassTwo
{
public AnotherClassTwo()
{
new OuterClass().super(); //accessing super class constructor through OuterClass instance
}
}
i have theses classes, why when extends from nested class we do not call outer class constructor , but when extends from inner class should call outer constructor through outer object, so what is the difference and why??
The reason is simple. To access a static property you don't need to create an object i.e. you don't need to instantiate the class.
But if you want to access a non static property you will first need to create an object of that class and then use it.
So here in your case when you want to extend InnerClassTwo(which is an inner class and non static) you will have to associate it with the constructor of the outer class AnotherClassTwo, as it can be thought of as a property of that outer class.
Related
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Java: Static vs non static inner class
What is a static nested class?
What is the difference between static and non-static nested classes?
A static inner class is a class nested inside another class that has the static modifier. It's pretty much identical to a top-level class, except it has access to the private members of the class it's defined inside of.
class Outer {
private static int x;
static class Inner1 {
}
class Inner2 {
}
}
Class Inner1 is a static inner class. Class Inner2 is an inner class that's not static. The difference between the two is that instances of the non-static inner class are permanently attached to an instance of Outer -- you can't create an Inner2 without an Outer. You can create Inner1 object independently, though.
Code in Outer, Inner1 and Inner2 can all access x; no other code will be allowed to.
This question already has answers here:
no enclosing instance of type... in scope
(2 answers)
Closed 5 years ago.
My code is as following :-
A class T can extend ClassO.Four. Four is a static class inside ClassO but cannot extend ClassO.Two Two is an inner class inside ClassO. I am getting error
No enclosing instance of type ClassO is available due to some intermediate constructor invocation
What is the reason of difference in behaviour?
class ClassO
{
interface inner
{
void msg();
}
class Two implements inner
{
public void msg()
{
System.out.println("Class Two");
}
}
static class Four
{
public void msg()
{
System.out.println("Class Four");
}
}
public void m()
{
}
}
class T extends ClassO.Two **// can extend ClassO.Four but not ClassO.Three**
{
public void msg()
{
System.out.println("Class Two");
}
}
Recall that a static inner class is a "normal" class with the name nested inside its owner class, and additional access privileges to private static members of its owner class. As a result, static inner classes have no restrictions as to the location from which they could be instantiated.
In contrast, a non-static inner class has access to both static and non-static members of its owner class. To make this possible, Java compiler embeds a hidden pointer to the owner into the nested class, and passes an instance of the owner to that constructor.
This behavior relies on the availability of owner's this at the point of instantiation of the non-static inner class. If compiler were to allow you to inherit from a non-static member outside its owner, it wouldn't be able to instantiate your derived class, in effect forcing it to behave as if it were an inner class. That is why the compiler prohibits such inheritance.
This question already has answers here:
Java inner class and static nested class
(28 answers)
Closed 7 years ago.
I have a class structure like this:
public class OuterClass {
private static class InnerClass {
public void someMethod() {
OtherClass.otherMethod(<???>);
}
}
which refers to a static method of some other class OtherClass:
public class OtherClass {
public static void otherMethod(OuterClass) {
....
}
}
I am trying to figure out what to put in place of the <???>. How do I refer to the instance of the outer class from within the inner static class? What I would like to do is to refer to the implicit this of the OuterClass.
You obviously need an object of OuterClass type:
public void someMethod() {
OuterClass oc = new OuterClass();
OtherClass.otherMethod(oc);
}
In case that your inner class is not static, then you could do:
//remove static here
private class InnerClass {
public void someMethod() {
OtherClass.otherMethod(OuterClass.this);
}
}
You should know the different between nested classes - static and non static. Static nested classes are simply classes like every other, just defined within other class (usually because of encapsulation principle). Inner static class instances have no knowledge of outer class instance.
Nested inner classes (non static) mandate that an object of the inner class exist within an instance of the outer class. That's why you can access it via OuterClass.this.
The simpliest way is to pass an instance of the outerClass in the constructor or in the method since the innerClass don't know this class.
like this:
public void someMethod(OuterClass outerClass) {
OtherClass.otherMethod(outerClass.myMethod());
}
I was learning about Nested and Inner classes and this led me to think whether it is possible to extend an Inner class to be a Nested class or not. For example.
public class Outer{
public class Inner{
// notice the lack of static keyword
}
}
public class ExtendedOuter extends Outer{
public static class ExtendedInner extends Inner{
// notice the static keyword
}
}
I did try to compile the code above and I couldn't, but the compile time error I received made me believe that there may be a work around. I can however extend a Nested class to be an Inner class.
This is the compile time error I received.
no enclosing instance of type Outer is in scope
An inner class has a reference to the outer class. You cannot remove it in a subclass. This would be like removing a field in a sub-class.
Actually you can extend the inner class. You just have to provide an instance of Outer that the class will be bound to. To do so, you have to explicitly call the super constructor with the instance.
public class Outer {
public class Inner{
// notice the lack of static keyword
}
}
public class ExtendedOuter extends Outer {
private static Outer outer = new ExtendedOuter(); // or any other instance
public static class ExtendedInner extends Inner {
public ExtendedInner() {
outer.super(); // this call is explicitly required
}
}
}
This also works if you have a nested class that extends another nested class from a different enclosing class.
Your question doesn't make sense. An inner class is already a nested class, and so is any other class defined inside another one. Evidently you don't know what these words mean:
nested class: a class declared inside another one
inner class: a nested class that isn't declared 'static'.
Note that 'static nested' and 'inner' are mutually exclusive. Note also that an inner class can extend a static nested class, but not vice versa.
What your code is actually trying to do is extend the inner class as a static class, which is what causes the error. Not because the extending class is nested.
This question already has answers here:
How to instantiate non static inner class within a static method?
(4 answers)
Closed 9 years ago.
we know that Static contexts can't reference any instance of any type, but what happens with main method, how the following code sample compiles with no problem:
public class MyOuter
{
public static void main(String[] args)
{
MyOuter mo = new MyOuter(); // gotta get an instance!
MyOuter.MyInner inner = mo.new MyInner();
inner.seeOuter();
//Or
MyOuter.MyInner inner = new MyOuter().new MyInner();
}
class MyInner
{
public void seeOuter(){}
}
}
isn't it forbidden to instantiate an inner class from within a static context in it's enclosing class?
isn't it forbidden to instantiate an inner class from within a static context in it's enclosing class?
No - it's forbidden to instantiate an inner class without an instance of the enclosing class. In your case, you do have an instance of the enclosing class:
new MyOuter().new MyInner();
That's entirely fine.
The only reason you can normally get away without specifying the enclosing class from an instance method is that it's equivalent to
// Within an instance method
this.new MyInner();
See section 15.9.2 of the JLS for more details. Your constructor call is a "qualified class instance creation expression".