• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Annonymous classes

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear friends,
1.
The Simon Roberts book on certification states:
" All annonymous inner classes are unique to method scopes, you cannot have anonymity with a memberclass " -- Does this means annonymous classes can be declared within methods only??
2. As per JQplus :::
Non-static inner classes can have static members -- True/False.
Well the answer is false.
They can have static fields which also have to be final. They can have any number of final fields. but they cannot contain static final methods. Can they contain just static methods or jsut final methods ?
Can anyone give me some piece of code on this?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Anonymous classes are declared as a parameter to a method call. That makes them local to the method that is being called.
obj.addActionListener(new ActionListener{//do cool stuff});
That guy in the parentheses is the anonymous class that is local to the addActionListener method.
2) Inner classes can not have static ANYTHING unless it is a compile time constant (static final). So they can't have staic methods at all (since a method can not be converted to a constant). However they can inherit static things.
 
padmini Babu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Cindy,
The point on static is clear to me now.
I hav e two questions:.
1.
what about an inner class within a static method. what fields it can access?
what about an static inner class. What fields it can access?
2. For anonymous clasess?
In pg 197 of Simon Roberts:
A form of anonymous class can also be declared in this way:
Xxxx AnXxxx = new Xxxxx{ /* class body */ } -- It is said that we can assign the reference to the constructed object into a variable.
Where can such form of anonymous class be declared? Please give me an example.
Thanks in advance.
Padmini
[This message has been edited by padmini Babu (edited June 24, 2001).]
 
padmini Babu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends,
Anybody out there? Scott, Cindy, Jane please help me.
Padmini
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.
what about an inner class within a static method. what fields it can access?
what about an static inner class. What fields it can access?
it can acess static instance variables of outer class and final local variables of the method in which it is declared
2. For anonymous clasess?
In pg 197 of Simon Roberts:
A form of anonymous class can also be declared in this way:
Xxxx AnXxxx = new Xxxxx{ /* class body */ } -- It is said that we can assign the reference to the constructed object into a variable.
Where can such form of anonymous class be declared? Please give me an example.

Runnable a=new Runnable(){public void run(){//code}};
Thread t=new Thread(a);
t.start();
 
padmini Babu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jeena jose:
[B]1.
what about an inner class within a static method. what fields it can access?
what about an static inner class. What fields it can access?
it can acess static instance variables of outer class and final local variables of the method in which it is declared
--------------------------------------
Hi jeena,
Thanks.
Is this the answer to both of the questions.?
Well one more question, jeena , what can static inner classes and non-static inner classes contain?
I have a lot of questions on static and final. . Any where i can get all that info???
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2. For anonymous clasess?
In pg 197 of Simon Roberts:
A form of anonymous class can also be declared in this way:
Xxxx AnXxxx = new Xxxxx{ /* class body */ } -- It is said that we can assign the reference to the constructed object into a variable.
Where can such form of anonymous class be declared? Please give me an example.
-------------------------------------------------------
They have said that anonymous class is local to a method. Is the above form local to a method?
Thanks padmini

 
padmini Babu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jeena jose:
[B]1.
what about an inner class within a static method. what fields it can access?
what about an static inner class. What fields it can access?
it can acess static instance variables of outer class and final local variables of the method in which it is declared
--------------------------------------
Hi jeena,
Thanks.
Is this the answer to both of the questions.?
Well one more question, jeena , what can static inner classes and non-static inner classes contain?
I have a lot of questions on static and final. . Any where i can get all that info???
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2. For anonymous clasess?
In pg 197 of Simon Roberts:
A form of anonymous class can also be declared in this way:
Xxxx AnXxxx = new Xxxxx{ /* class body */ } -- It is said that we can assign the reference to the constructed object into a variable.
Where can such form of anonymous class be declared? Please give me an example.
-------------------------------------------------------
They have said that anonymous class is local to a method. Is the above form local to a method?
Thanks padmini

 
knowledge is the difference between drudgery and strategic action -- tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic