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

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)

Related

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

inner class extended by another inner class [duplicate]

This question already has answers here:
Why does a sub-class class of a class have to be static in order to initialize the sub-class in the constructor of the class?
(2 answers)
Closed 5 years ago.
Error occur when class C extends B. But, when I write new A().super(); problem is solved. Please consider following code:
public class A {
public class B extends A {
public class C extends B {
public C() {
// No enclosing instance of type A is available due to some intermediate constructor error
// new A().super();
}
}
}
}
My question is why class C cannot extend Class B? Why calling new A().super(); solved the problem? What does it mean? Is there better way to solve it (without using static nested class)?
Simplest code snippet which will be compiled and executed printing 'OK':
public class A {
public class B extends A {
public class C extends B {
public C() {
System.out.println("OK");
}
}
}
public static void main(String[] args) {
new A().new B().new C();
}
}
Here is another example of instantiation of A, B and C. That is, class C can extend Class B as of initial code snippet from your question. Your code is correct in terms of syntax, and can be compiled without adding any unnecessary new A().super()
For consideration, let's refer to the tutorial for inner classes, those by definition are non-static. For static case the correct name is static nested class
An instance of InnerClass can exist only within an instance of OuterClass
To instantiate an inner class, you must first instantiate the outer class
That means, that inner class exists only in context of particular OuterClass instance rather than OuterClass class, that's why new A() does solve the problem, providing runtime instance within which classes B and C do exist

Why outer classes are not static in java? [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.
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();
}
}
}

Java error "Non static variable cannot be referenced from a static context" [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
java : non-static variable cannot be referenced from a static context Error
My aim is to create a program for client server chat.I wrote the following code https://github.com/jinujd/Java-networking/blob/master/Server.java
for the server.. After compilation I got the following error.
non-static variable this cannot be referenced from a static context.
What is the problem there?
My another doubt is that
/*A.java*/
class A {
String a;
class B {
}
public static void main() {
}
}
Is the variable a accessible to B and main() ?
Static functions/variables are associated with the class definition itself while class variables(non-static) are associated with class instance i.e. they are normally initialized when you instantiate an object from the class.
Static functions/variables can be used without class instance as:
A.main();
While to access non-static functions/variables, you need to create object instance first:
A a = new A();
a.getA();
Since static scope is up in the hierarchy(at definition level), and it doesn't have visibility of instance level methods/variables and hence complains. But opposite is OK i.e. you should be able to access static methods/variables from non-static methods.
Having explained the reason, I believe you would be able to correct the scope of the class/method/variable yourself.
You need
static class ClientReceiver extends Thread {
Not
class ClientReceiver extends Thread {
non-static variable this cannot be referenced from a static context. What is the problem there?
you need an instance of the class to access the non-static data from static context.
public class Sample {
String var="nonstatic variable";
public static void main(String...args){
Sample s= new sample();
system.out.println(s.var);
}
}
your class B can access your string a directly, but your static main method needs an instance of class A to access it.

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