aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class question in Rules Round-up Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class question in Rules Round-up" Watch "Inner class question in Rules Round-up" New topic
Author

Inner class question in Rules Round-up

larry
Greenhorn

Joined: Apr 19, 2001
Posts: 2
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

Joined: Feb 04, 2001
Posts: 400
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

Joined: Mar 17, 2000
Posts: 5782
Your name 'larry' does not comply with the JavaRanch naming policy. Please choose one that meets the requirements.
Thanks!


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
larry
Greenhorn

Joined: Apr 19, 2001
Posts: 2
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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner class question in Rules Round-up
 
Similar Threads
rules round up question.
Why instantiate a static inner class?
Inner Classes Q
Rules Round Up Game ( Question No.235)
Inner class