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
posted
0
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
posted
0
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.