wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonymous Class" Watch "Anonymous Class" New topic
Author

Anonymous Class

Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi,
Can Anonymous class be static?
In Khalid & Rolf's book (p.no 243) they say, if a static method returns an anonymous class then it would be static.

The context determines whether the anonymous class is static, and the keyword static is not used explicity. For example, an anonymous class as the return value of a static method would be static, as it would be if it was used to initialize a static member variable.
.
JLS says,

An anonymous class is never abstract (�8.1.1.1). An anonymous class is always an inner class (�8.1.2); it is never static (�8.1.1, �8.5.2). An anonymous class is always implicitly final (�8.1.1.2).

can anyone clear this doubt?
Thanks,
Vanitha.
Caven Wang
Ranch Hand

Joined: Jul 07, 2001
Posts: 31
JIS means an Anonymous class can not be declared static. But it could be "default", static, which is equal to the context property.
[This message has been edited by caven (edited July 07, 2001).]


@_@ Life is so nice!
Anshul Manisha
Ranch Hand

Joined: Apr 17, 2001
Posts: 74
Hi Vanitha take a look at Paul's comment in this posting. http://www.javaranch.com/ubb/Forum24/HTML/010399.html


AM<BR> <A HREF="mailto:anshulmohan@rediffmail.com" rel="nofollow">anshulmohan@rediffmail.com</A>
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Thanks for your reply Anshul. I read Paul's comment, I tried to create an anonymous class in a static method and an instance method. Then did javap, but it said the classes are final.

C:\Vanitha\SCJP2\Testprog\KM\chap7>javap AnonyTest
Compiled from AnonyTest.java
public class AnonyTest extends SuperA {
public AnonyTest();
static SuperA aMethod();
SuperA bMethod();
}
C:\Vanitha\SCJP2\Testprog\KM\chap7>javap AnonyTest$1
Compiled from AnonyTest.java
finalclass AnonyTest$1 extends SuperA {
int m;
AnonyTest$1();
}
C:\Vanitha\SCJP2\Testprog\KM\chap7>javap AnonyTest$2
Compiled from AnonyTest.java
final class AnonyTest$2 extends SuperA {
int n;
AnonyTest$2();
}
Can anyone throw some light on this?
Vanitha.
Anshul Manisha
Ranch Hand

Joined: Apr 17, 2001
Posts: 74
I too got similar kind of results. Now if we go by javap results as a conclusive evidence of determining if a class is static or not then it seems anonymous classes are not static(then where would they be static) Can anyone show an example of static anonymous classes.
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi Anshul,
I created a non-static inner class inside a static method, and tried to access a instance variable of the enclosing class. Eventhough it a non-static innerclass it can't access the instance variable of the enclosing class, since it is inside the static method.
So, I think there is no static anonymous class, if it is created inside the static method, then it is treated as static anonymous, but actually it is not.
See the code, it is clumsy look at the bottom of the code.

correct me if I am wrong, i just tried an example code, this is not a conclusion.
Vanitha.
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Vanitha, I think the class you have defined as sM is indeed a static class (it is not anonymous, and it is defined inside a static method). I believe that's why it can't access the instance variables of the enclosing class.
I haven't seen anything that conclusively demonstrates the possibility of a non-final or static anonymous class. Until that is demonstrated, I'd have to think the JLS is correct and Khalid's statement is incorrect.
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi Scott,
As I mentioned earlier in my post, I tried to define a inner class (not static) inside a static method. It (non-staic inner class)can't access the non-static variable outside the method.
Vanitha.
Anshul Manisha
Ranch Hand

Joined: Apr 17, 2001
Posts: 74
Hi Vanitha,
First int x would be accessible in sM because x is declared as static int in class AnonyTest. I did javap on class sM and it gave me following output

Compiled from AnonyTest.java
class AnonyTest$1$sM extends java.lang.Object {
int smA;
AnonyTest$1$sM();
}

This again does not explicitly state that class is static so I am
Khalid Mughal
Author
Greenhorn

Joined: May 21, 2001
Posts: 3
JLS quote:
"An anonymous class is never abstract (�8.1.1.1). An anonymous class is always an inner class (�8.1.2); it is never static (�8.1.1, �8.5.2). An anonymous class is always implicitly final (�8.1.1.2)."
If you look carefully you will see that the word "static" refers to the keyword in the above quote. I choose to interpret this as meaning that an anonymous class cannot be declared static (<--- keyword).
In our explanation, we are using the word "static" in the non-keyword sense of the word to indicate "an anonymous class declaration in a static context". As the examples presented in this discussion have shown, an anonymous class declared in a static context cannot access instance members in its enclosing context. This is the point we wanted to get across and the fact that such a class is instantiated without any outer object.
We will make sure that such confusion about terminology does not arise in any future edition of the book.
wbw,
khalid mughal
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Thanks Khalid for your clarification.
Vanitha.
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
I still don't quite understand. The inner class within a static method could not access instance variables because that inner class was considered "static" or just that we could not use instance fields in a static methods?
I see Khlaid pointed out that such a class is created without the outer object.
Thanks. Nevermind.
[This message has been edited by Cameron Park (edited July 09, 2001).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Anonymous Class
 
Similar Threads
Local and Anonymous class can be static or non-static
Dan Chisholm Chapter 4, question 15
Couple of questions
Anonymous Class
Inner class