About Question enthuware.ocpjp.v7.2.1357 : Can you have stat
Moderator: admin
-
- Posts: 31
- Joined: Tue Oct 06, 2015 1:57 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1357 : Can you have stat
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??
I wanted to know if the opposite is also true? My guess is that you cannot have a static method within an anonymous class??
Online
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1357 : Can you have
What happened when you tried it out?
If you like our products and services, please help us by posting your review here.
-
- Posts: 28
- Joined: Wed Feb 25, 2015 9:03 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1357 : Can you have
according to JLS (http://docs.oracle.com/javase/specs/jls ... jls-15.9.5):
more about: http://stackoverflow.com/questions/7585 ... ava-static
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: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 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.An anonymous class is always an inner class
more about: http://stackoverflow.com/questions/7585 ... ava-static
Online
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1357 : Can you have
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:
Now, if I do javap on the two nested classfiles, both are exactly the same:
HTH,
Paul.
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();
}
}
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();
}
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 28
- Joined: Wed Feb 25, 2015 9:03 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1357 : Can you have
You are right.
Tnx Paul
Tnx Paul
-
- Posts: 19
- Joined: Wed May 25, 2016 4:33 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1357 : Can you have
Hello,ThufirHawat wrote:according to JLS (http://docs.oracle.com/javase/specs/jls/se7/html/jls-
An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.1).
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!
Online
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1357 : Can you have
You got it correctly. They are stating the obvious.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: admin and 203 guests