Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

anonymous?

 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An anonymous inner class can have a static initializer . Is this statement true or false ?

Options :

a . true
b . false

initializers has nothing to do with the name ...so why cant it have???
 
Joan Pujol
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler says that this is not possible
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by srikanth reddy:
An anonymous inner class can have a static initializer . Is this statement true or false ?

Options :

a . true
b . false

initializers has nothing to do with the name ...so why cant it have???

What sense does it make to have a static initializer in an anonymous inner class?
 
Ray Horn
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so why cant it have?

anonymous inner class is an instance of an class. Static code cannot access instance members.
 
Abhijit Sontakey
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Anonymous class is a local member of a method or constructor. It is not a class level member. Just as a local method variable cannot be static a anonymous class reference cannot be declared static.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Static initializer-No ..

reasons have been given in the posts....


instance initializers-Yes...


The non-static initializer is the only way to do initialization in an anonymous inner class, as those can't have constructors.



Regards
 
agrah upadhyay
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just Remember one very important point that static construct i.e. static variable,initializer,method etc are top level construct so they can appear at toplevel class scope or nested within a static construct.
Hope it helps
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic