Page 1 of 1
About Question enthuware.ocpjp.v7.2.1357 : Can you have stat
Posted: Sat Oct 10, 2015 4:39 pm
by krohani
So I understand the explanation for this question that an anonymous class CAN be declared within a static method and also a static class.
I wanted to know if the opposite is also true? My guess is that you cannot have a static method within an anonymous class??
Re: About Question enthuware.ocpjp.v7.2.1357 : Can you have
Posted: Sat Oct 10, 2015 7:45 pm
by admin
What happened when you tried it out?
Re: About Question enthuware.ocpjp.v7.2.1357 : Can you have
Posted: Mon Nov 16, 2015 12:22 pm
by ThufirHawat
according to JLS (
http://docs.oracle.com/javase/specs/jls ... jls-15.9.5):
15.9.5. Anonymous Class Declarations
An anonymous class declaration is automatically derived from a class instance creation expression by the Java compiler.
An anonymous class is never abstract (§8.1.1.1).
An anonymous class is always implicitly final (§8.1.1.2).
An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.1).
So, officially, you can't have an anonymous class inside a static context. That class of the question, declared in a static method, is declared in a static context.
Very important here:
An anonymous class is always an inner class
So that means that you ALWAYS MUST have an outer instance to be called from anonymous class. That class, of the question, can't be called as an anonymous class, but a Static Nested Class.
more about:
http://stackoverflow.com/questions/7585 ... ava-static
Re: About Question enthuware.ocpjp.v7.2.1357 : Can you have
Posted: Mon Nov 16, 2015 8:10 pm
by admin
JLS says an anonymous class cannot be static. It doesn't say that it can't be created in a static context.
Both can be interpreted differently.
As a test, I compiled the following code:
Code: Select all
interface X{
public void m();
}
public class TestClass {
static class Y implements X{
public void m(){
System.out.println("in m");
}
}
public static void main(String[] args) {
X x = new X(){
public void m(){
System.out.println("in m");
}
};
x.m();
}
}
Now, if I do javap on the two nested classfiles, both are exactly the same:
Code: Select all
C:\temp>javap TestClass$Y.class
Compiled from "TestClass.java"
class TestClass$Y implements X {
TestClass$Y();
public void m();
}
C:\temp>javap TestClass$1.class
Compiled from "TestClass.java"
final class TestClass$1 implements X {
TestClass$1();
public void m();
}
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1357 : Can you have
Posted: Mon Nov 16, 2015 10:32 pm
by ThufirHawat
You are right.
Tnx Paul
Re: About Question enthuware.ocpjp.v7.2.1357 : Can you have
Posted: Mon Jun 20, 2016 2:02 pm
by dieterdde
Hello,
could someone confirm exactly what the relevancy is of that statement? I'm not understanding the message from that sentence.
Since an anonymous class is instantiated like this.
Dog dog = new Dog() {
...
};
How could the anonymous class ever be "static"? It's not possible to type static anywhere here right, so I'm not sure if they're just stating the obvious or I'm missing something.
What I do know is that there is no issue to instantiate an anonymous class within a static context (as Paul explained as well), and also is it perfectly possible to make an anonymous class from a static class or to make a static variable point to an anonymous class object instance like:
public class Tester {
static Anon anon;
static class Anon {
public void doIt() {
System.out.println("doIt");
}
}
public static void main(String[] args) {
anon = new Anon() {
@Override
public void doIt() {
System.out.println("anonymous doIT");
}
};
anon.doIt();
}
}
What am I missing here?
Thanks!
Re: About Question enthuware.ocpjp.v7.2.1357 : Can you have
Posted: Mon Jun 20, 2016 8:25 pm
by admin
You got it correctly. They are stating the obvious.