• 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

how to access a method from static intializer block

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
plz help me out!!!
----------------------------------------------
public class MyClass{
static{
class A{
public void callMe(){System.out.println("Java");}
}
}
public static void main(String[] args){
//Here
}

what should i write at the position "//Here" so that the prog. prints "Java"
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't. Class A is a local class, and can only be used when in scope (in this case the static initialization block). It's like trying to use a local variable from a different method than where the variable was declared.

I guess that what you wanted to do is:



...Ariel
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Akshay Singhvi:
... what should i write at the position "//Here" so that the prog. prints "Java"


The trick answer is...

Otherwise, Ariel is correct. The local class is simply not in scope.
reply
    Bookmark Topic Watch Topic
  • New Topic