This question already has answers here:
Access outer class from inner class: Why is it done this way?
(3 answers)
Referencing an enclosing instance from an inner class
(1 answer)
Closed 7 years ago.
Can someone explain the way of using this in AbstractList sources:
AbstractList.this.remove(lastRet);
This line is in the remove() method of the private class Itr implements Iterator<E> inner class.
Inner classes (as opposed to static nested classes) are always associated with an instance of the surrounding class.
Since they are members of the surrounding class, they have access to all its members, private or public. In order to access a member of the surrounding class, especially if the member name may be ambiguous, you need to access the "this" of the surrounding class instance.
The syntax used is the name of the surrounding class with .this. Which is exactly what you see in your example. AbstractList is the surrounding class, and AbstractList.this is the instance of the surrounding class associated with the current instance of Itr.
The syntax ClassName.this works in nested non-static classes. It lets you access the reference to the object that "owns" the nested class, i.e. this object of the parent object.
This is the case of the iterator class inside the abstract list: it calls remove method on its parent AbstractList object.
Related
This question already has answers here:
Keyword for the outer class from an anonymous inner class [duplicate]
(2 answers)
Closed 7 years ago.
Say I have the follow code:
class Outer {
int n;
class Inner {
}
}
Is it possible for an object of type Inner to access the instance variable n of its Outer object? (When I say its, I mean the Outer object that its associated with.) I understand that within a method of an inner class you can access outer class instance variables and also within the body (for example an instance variable for Inner could initialize to the value of an instnace variable of Outer), but is it possible to more directly access the Outer instance variables? For example, if inner were an object of type Inner, is there anything like inner.n given the code above?
Sorry for the block of text: basically, if inner were an object of type Inner, is there anything like inner.n given the code above?
Sure. Non-static inner-classes are generated in a way that allows them to access any variable in the respective outer-class (which is reason for quite some security-issues). Instances of non-static inner classes can only be generated within a given instance of the outer class and hold a reference to the outer class that allows them access to any variable of the outer class.
For more infos on this topic just read the java language reference.
Is it possible for an object of type Inner to access the instance variable n of its Outer object?
Yes, and you can access the fields of other instances of that class as well, even if they are private.
I understand that within a method of an inner class you can access outer class instance variables and also within the body (for example an instance variable for Inner could initialize to the value of an instnace variable of Outer), but is it possible to more directly access the Outer instance variables? For example, if inner were an object of type Inner, is there anything like inner.n given the code above?
You can't access the fields which are visible to the Inner class from another class. Only the inner and out class can access them, unless the fields are public or accessible via normal access modifiers.
basically, if inner were an object of type Inner, is there anything like inner.n given the code above?
Inner doesn't have a field called n so you can't access it using that name even via reflection. What you can do is access Outer.this.n in the inner class, and you can use reflection to get the instance of the Outer class and get it's n field.
This question already has answers here:
Use of class definitions inside a method in Java
(8 answers)
Closed 7 years ago.
class A
{
{
class B
{
// What is the use of an inner class inside IIB?
}
}
}
We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable.
Additionally, it can access all the members of outer class including private data members and methods.
A member class is also defined as a member of a class. Unlike the static variety, the member class is instance specific and has access to any and all methods and members, even the parent's this reference.
more info here and here
This question already has answers here:
Why are interfaces static?
(5 answers)
Closed 5 years ago.
as the title says, why nested interfaces are implicitly static.Why cant i use nested interfaces just like nested classes ie like with static and without static
From the oracle documentation:
an inner class is associated with an instance of its enclosing class
and has direct access to that object's methods and fields
i.e. it maintains a reference to its enclosing instance.
An interface on the other and is just a static definition of type. A contract, used to provide some polymorphism.
An interface cannot itself be instantiated, therefor it cannot have state, and thus it cannot "know" about an enclosing instance.
Java interface are interfaces. A interface rule is ACID. The I in ACID means:
To demonstrate isolation, we assume two transactions execute
at the same time, each attempting to modify the same data.
So fields are automatically public static final.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java inner class and static nested class
An instance of a static inner class cannot access the instance members of its enclosing class, whereas an instance of a non-static inner class can. This is what i mean by syntactic difference. Because whether declaring an inner class to be static determines whether the syntax of your program is correct.
But is there any other difference that's not part of the Java syntax? Let's say class A is a top-level class, and class B is an inner class of A. If I'm not going to access the instance members of A within B, then I should declare B to be static. But since i'm not required to, i could declare B to be non-static and there would be no compilation error. So in this case, is there any difference, probably in the generated bytecode, or any runtime difference?
Thanks!
The difference is bigger than that. static inner classes can be created from outside the class, without having an instance of the class, non-static ones cannot.
The fact that you can access enclosing class members is a result of this, because a static inner class is not bound to an instance of the enclosing class, but a non-static one is.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why cant we have static method in an inner class?
Hi all, in java what exactly is the reason that we cannot declare static methods in [public] inner classes unless those inner classes are also declared static?
Amazingly top level classes can have any number of static methods without the need to have any special modifier
Non-static inner classes come into existence only in the context of an instance of the outer class.
So ... if you're going to have a static method, the whole inner class has to be static. Without doing that, you couldn't guarantee that the inner class existed when you attempted to call the static method.
The question to ask is -- if you do have a static method inside an inner class, how would you call that static method? The answer is, you can't.
An inner class is tied to instances of the outer class.
From Effective Java -- "Each instance of a nonstatic [nested] class is implicitly associated with an enclosing instance of its containing class".
This is the reason for making the "inner" class static. This actually a static nested class and its a full-blown class thats merely present in the enclosing class for packaging convenience.