Why outer classes are not static in java? [duplicate] - java

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.
In java, An outer class may be public, final, default or abstract.
Why not Static like
public static class MyClass{}

An outer class is already implicitly static.
Non-static nested class (= inner class) means thats the inner class implicitly has a reference to its parent class.
That's why, for nested class, you can distinguish between static and non-static. This does not make sense for outer classes.
Here is an example to understand the difference between static/non-static nested class. You should understand why it does not make sense in an outer class.
public class MyClass {
private String anAttributeOfMyClass;
private /*static*/ class MyInnerClass {
public void foo() {
/*
* Here, I can access the attribute of the parent class
* because I implicitly have a reference to it.
* Try to make the nested class static an see the difference.
*/
anAttributeOfMyClass.trim();
}
}
}

Related

how to refer same method from (non static) inner to outer class [duplicate]

This question already has answers here:
Getting hold of the outer class object from the inner class object
(7 answers)
Closed 2 years ago.
I have a (non static) inner class and have a same method within both the inner and the outer class. How can I call the outer method within the inner method?
class User{
public void call() {
...
}
public class Admin{
public void call() {
// I want to refer to User#call, not to Admin#call()
// super.call() does not work here, because no inheritance
call(); // refers to Admin#call
}
}
}
By applying the class name to "this":
User.this.call()

Java Nested Class: Instantiating inner class from outer class [duplicate]

This question already has answers here:
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 3 years ago.
When instantiating an inner class in Java why do I need to create a new reference to it? In the first example code a reference to Inner is made, then using that reference there is an attempt to instantiate class Inner() which doesn't work but in the second example code (where a reference to Inner is still made), the instantiation of class Inner() is successful because instead of "inner", "Inner inner" was used. So to my (noob) understanding, a new reference had to be made?
public class Outer{
Inner inner;
private class Inner{}
public static void main(String[] args){
Outer outer = new Outer;
inner = outer.new Inner(); // doesn't work (only difference in code)
}
}
public class Outer{
Inner inner;
private class Inner{}
public static void main(String[] args){
Outer outer = new Outer;
Inner inner = outer.new Inner(); // works (only difference in code)
}
}
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.
From the first code, you are trying to instantiate a non-static variable in a static method which is not allowed.
But the second snippet, you are making the instantiation locally within the method (which does not affect the value of the variable outside the method). So the second snippet works in JAVA

Static inner class extending abstract outer class in java [duplicate]

This question already has answers here:
Java inner class and static nested class
(28 answers)
Closed 5 years ago.
I saw this pattern today and it confused me a lot:
abstract class A {
// does something
static class B extends A {
// does something as well
}
}
Two weird things I found about it:
Static class can be initialised using new A.B().
Static class is not unique in the application (therefore, not really static), as each initialisation creates a new object.
I am still perturbed as to why to use such a pattern? And does static class in this context mean, that you can access it's constructors statically, without needing to create an instance of A, but not really it being a unique in any way in the app?
EDIT:
OK, so I think my understanding of static classes came from C#. I am clear on the staticness of java classes now. But when would you use such a pattern (where inner static class is extending outer abstract one) and why?
static class doesnt have access to the outer class methods and variables, they keyword kind of means that it is a separated class.
class Out {
int i; void x(){}
static class In() {
i++; x(); // not valid instructions
}
class In2() {
i++; x(); // valid instructions
}
}
To instantiate a static inner class you just create a object of it:
Out.In obj = new Out.In();
non-static needs a instance of the outer class to be instantiated with:
Out o = new Out();
Out.In2 obj = new o.In2();
(If instantiating In2 inside of Out the word this is implicit)

Inheritance behaviour in java when both child and parent classes are inner [duplicate]

This question already has answers here:
Why do inner classes make private methods accessible?
(4 answers)
Closed 6 years ago.
Why is the private method of the parent class Base visible in the child class Child in the code below?
public class Trial {
class Base {
private void foo()
{
}
}
class Child extends Base {
private void func()
{
super.foo();
}
}
}
It wouldn't be possible if Base and Child classes were not inner classes. Why is this behaviour for inner classes?
[If] the member or constructor is declared private, [then] access is permitted if and only if it occurs within the body of the top level class (ยง7.6) that encloses the declaration of the member or constructor.
- https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#d5e9880

How do you get a reference to the enclosing class from an anonymous inner class in Java? [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'm currently creating an explicit reference to this in the outer class so that I have a name to refer to in the anonymous inner class. Is there a better way to do this?
I just found this recently. Use OuterClassName.this.
class Outer {
void foo() {
new Thread() {
public void run() {
Outer.this.bar();
}
}.start();
}
void bar() {
System.out.println("BAR!");
}
}
Updated If you just want the object itself (instead of invoking members), then Outer.this is the way to go.
Use EnclosingClass.this
You can still use Outer.class to get the class of the outer class object (which will return the same Class object as Outer.this.getClass() but is more efficient)
If you want to access statics in the enclosing class, you can use Outer.name where name is the static field or method.

Categories