Inner class in Instance Initialization Block in Java [duplicate] - java

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

Related

Can I access the "secondary this" from an inner class? [duplicate]

This question already has answers here:
Getting hold of the outer class object from the inner class object
(7 answers)
Closed 6 years ago.
I have basic classes
abstract class Unit {
Unit target;
abstract class UnitAI {/*...*/}
}
From these, I have derived
class Infantry extends Unit {
class InfantryAI extends UnitAI {/*...*/}
}
Can the class InfantryAI somehow get the secondary(implicit) this that is used for accessing the members of it's surrounding class Infantry?
Specifically, it needs to determine if its surrounding class Infantry is being targetted by its target, like this:
if (/*secondary_this.*/target.target == secondary_this)
or, generally, by another Unit.
You can access the outer this by prepending the classname:
Infantry.this.target; //"this" of the Infantry class from inside InfantryAI
Unit.this.target; //"this" of the Unit class from inside UnitAI
This doesn't work with static nested classes though as they don't belong to a instance of the outer class.

Strange line of code in AbstractList source [duplicate]

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.

Why are nested interfaces declared implicitly static? [duplicate]

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.

What are the non-syntactic differences between static and non-static inner classes in Java? [duplicate]

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.

what exactly is the reason that we cannot declare static methods in [public] inner classes unless those inner classes are also declared static?azi [duplicate]

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.

Categories