• 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

class in static initializer

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can any one explain how can we call the method of a class declared inside the static initializer.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic