• Post Reply Bookmark Topic Watch Topic
  • New Topic

class in static initializer  RSS feed

 
tarunya rastogi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can any one explain how can we call the method of a class declared inside the static initializer.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

You can't do anything with def outside of static block of abc.
You need to declare def outside of static block of abc.

If you really wanted you could:
1. Have def implement an interface.
2. Create a static variable of the type the interface in abc;
3. Assign an instance of def to the variable.

But I don't see a reason to do this. Just define def as a static nested class of abc.

By the way, you should use meaningful names for your classes.
 
tarunya rastogi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Welcome to the Ranch!

You can't do anything with def outside of static block of abc.
You need to declare def outside of static block of abc.

If you really wanted you could:
1. Have def implement an interface.
2. Create a static variable of the type the interface in abc;
3. Assign an instance of def to the variable.

But I don't see a reason to do this. Just define def as a static nested class of abc.

By the way, you should use meaningful names for your classes.


Thanks a lot Paweł Baczyński.

i will use meaningful name for my classes.

 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

This is a common problem; except for fields all variables have a limited scope, the block they are declared in. If you declare that class in the initialiser block, it has no recognisable existence outside that block.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!