Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner class question in Rules Round-up

 
larry
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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();
}
}
 
Stevie Kaligis
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
  • 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
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • 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
  • 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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic