Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Inner class question in Rules Round-up  RSS feed

 
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();
}
}
 
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
 
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


 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!