• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Anonymous Class

 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vanitha take a look at Paul's comment in this posting. http://www.javaranch.com/ubb/Forum24/HTML/010399.html
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Khalid for your clarification.
Vanitha.
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic