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

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.

Related

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.

Inner class in Instance Initialization Block in Java [duplicate]

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

can we create static class in Java web application? [duplicate]

This question already has answers here:
Why are you not able to declare a class as static in Java?
(14 answers)
Closed 9 years ago.
I am trying to create static class in Java Web Application using eclipse. but it says
Illegal modifier for the class ClassName; only public, abstract & final are permitted
can we create static class in web application ? if no why ?
No, because there are no static top-level classes in Java.
Static class you can create in general only as nested class in another class. This has nothing to do with Web-Application. It has a general validity in Java.
Each compilation unit should contain a public or default (non-static) class. Then within it you can declare static nested classes.
Only inner classes can be made as static.
That means except inner class you can not create static class
A static class is, in practice, nothing else than a standard non-inner class declared in another class.
In other words, your app web application has nothing to do with classes being declared as static or not.
Outer class cannot be static. Only nested class can be static. For example below is possible
class OuterClass {
static class StaticNestedClass {
...
}
}
But not
static class OuterClass {
...
}

why do we declare a static class inside normal class? [duplicate]

This question already has answers here:
Java: Static vs inner class [duplicate]
(8 answers)
Closed 9 years ago.
What is the purpose of defining a static class inside a normal class.
public class ClassA extends ClassB implements IA, IB {
public static classStatic extends ClassC implements I1, I2 {
}
}
What is the purpose of defining a static class inside a normal class.
Do all fields/variables need to be static, if its accessing inside the static class.
Can anyone explain/show article me what exactly is achieved in oops by implementing this in JavaScript way.
In Java you can only have one public/package only class per file; that class may require some computation/logic that is to complex and would fit better in another class but it is not general enough to be in another file, it is tight to the encompassing class.
If the inner class wouldn't be static, then you would need an instance of the encompassing class (in your case ClassA) to instantiate the inner class ClassC.
No they do not need to be static; it is a class like any other class from this point of view.
JavaScript is not a class based object-oriented language, so you cannot have classes of any kind.

Why and when to use static inner class or instance inner class? [duplicate]

This question already has answers here:
Static nested class in Java, why?
(14 answers)
Closed 9 years ago.
I have been studying about static inner class in java. But i am not clear whats the point of using static inner class or inner class.
class A{
static class B{
}
public static void main(String[] args) {
B b=new B();
}
}
or
class B{}
class A{
public static void main(String[] args) {
B b=new B();
}
}
Non-static inner classes have an automatic reference to their enclosing class. A static inner classes only relationship to its enclosing class is that you have to reference it via the enclosing class' name: EnclosingClass.StaticInnerClass.
Non-static inner classes are good when you want to reference some of the data from the parent class.
A static inner class is good when you just want to associate the inner class with the enclosing class without dragging it along for the ride.
In other words, a non-static inner class can prevent the enclosing class from being garbage collected, since it has that reference, while a static inner class will never do that.
There is technical difference:
class A {
private static int x = 42; //the answer
public static class B {
int showX() {
return x; // only static class can it
}
}
}
But it isn't the main point. If class B is used only by class A it's good to make it inner because some classes in one package may want to have utility class with same name.
The result is the same, but if B is a little Class that A uses it just makes more sense to put it into A.
By making a nested classes can be static, you can use the nested class without having an instance of the outer class.
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.
...
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.
See tutorial here
When your inner class is considered as part of your "object", use inner class.
Indeed, you would be able to access private,package,protected and public fields from your wrapping class.
"Drawback" is: An inner class can't exist without it's wrapping class instantiated, that is logically due to the first sentence.
Otherwise, if you consider:
The behaviour of your nested(called also static inner) class isn't considered as reusable by external class since maybe too specific.
some related fields of one class as being so much related that you want to make
a class wrapping them. This will get your code more understandable and cleaner.
then choose to make a static class.
Moreover, since static (meaning outside the life cycle of any object), a nested class may instantiate without regarding its wrapping class/object.

Categories