• 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

Inner class question in Rules Round-up

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, guys
need your help!
question:
It is impossible to have an instance of a non-static inner class before any instance of the outer class have been created.
It's in Rules Round-up mock. The answer is True.
But imagine a scenario that a local inner class is in a static method, then its instance can be used without any instance of the outer class, like the code follows. Is there anything wrong?
class TestInner{
static void methodA() {
class inner{
inner(){ System.out.println("inner init"); }
};
inner i=new inner();
System.out.println("methodA");
}
}
class Test{
public static void main(String[] args){
TestInner.methodA();
}
}
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is nothing wrong....
this is called "local class", when you define an inner class inside static context(i.e static method or a static initializer), then it will implicitly static.
stevie
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your name 'larry' does not comply with the JavaRanch naming policy. Please choose one that meets the requirements.
Thanks!
 
larry
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, Stevie
I'm still confused. You see, there are 4 kinds of inner class: static, member, local and anonymous. In my example, we got the instance of local inner class before any instance of outer class have been created. Do you mean the answer is wrong? Or do you mean the local inner class inside static method is static inner class?

Originally posted by Stevie Kaligis:
There is nothing wrong....
this is called "local class", when you define an inner class inside static context(i.e static method or a static initializer), then it will implicitly static.
stevie


 
please buy my thing and then I'll have more money:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic