| Author |
inner class
|
Niala Nirell
Ranch Hand
Joined: Mar 12, 2008
Posts: 46
|
|
someone can explain me this : "no inner class can have a static member" it's from the scjp FAQ and i don't find any reference of this ine te Kera & Bates book thank you
|
 |
yu yong
Ranch Hand
Joined: Mar 09, 2008
Posts: 44
|
|
Hi Alain: I think there are some mistakes on it. The code which has a inner class using a statci member can compile and run successfully. class InnerClass { static final String A = "A"; } public class StaticTest { public static void main(String[] args) { System.out.println(InnerClass.A); } } The result is : A
|
Yours sincerely,<br />yuyong<br /> <br /> <br /> E-mail:yuyong22@hotmail.com<br /> msn: yuyong22@hotmail.com<br /> Skype:yuyong88
|
 |
Niala Nirell
Ranch Hand
Joined: Mar 12, 2008
Posts: 46
|
|
did you mean? : public class Test { class InnerClass { static final String A = "A"; } public static void main(String[] args) { System.out.println(InnerClass.A); } } Anyway, it is still possible to declare a static member into the inner class.
|
 |
Afzal Hossain
Greenhorn
Joined: Jan 02, 2006
Posts: 25
|
|
Hi, "no inner class can have a static member" Yes, that's true. I dont see any reason to use static member in inner class. Only Top level and static nested class can have static member. thanks Afzal
|
Afzal
|
 |
Niala Nirell
Ranch Hand
Joined: Mar 12, 2008
Posts: 46
|
|
|
my example compile perfectly and you have a static member into an inner class. i still don't understand this afirmation.
|
 |
Nageswar Kakolla
Ranch Hand
Joined: Jan 16, 2006
Posts: 71
|
|
Guys, static final String A = "A"; is allowed since static final is way of defining CONSTANTS in java. whereas static variable is not allowed in inner class. Try static String A ="A"; and you will get compiler error since above String A is static variable and is not allowed in inner class due to Instance is required for outer class and static variables are not applicable for Instance classes. Hence Non static Inner classes can not contain static member variables but constants are allowed. Hope this explanation suffice !
|
 |
Niala Nirell
Ranch Hand
Joined: Mar 12, 2008
Posts: 46
|
|
|
thank you, it's more clear now
|
 |
Dinesh Tahiliani
Ranch Hand
Joined: Aug 06, 2007
Posts: 486
|
|
Hi, You mean to say that we can define static variables in subclasses not in Inner class. please shed some lights on this. I am not undertanding the concept.
|
Thanks<br />Dinesh
|
 |
Niala Nirell
Ranch Hand
Joined: Mar 12, 2008
Posts: 46
|
|
maybe you will understand with an example: public class Test { class InnerClass { static String A = "A"; } public static void main(String[] args) { System.out.println(InnerClass.A); } } this code does not compile. static means that the variable is a class member and not an instance member. Or in order to access an inner class you need an instance of the outer class, so the concept of static member for an inner class is not valid anymore. It's my understanding.
|
 |
Dinesh Tahiliani
Ranch Hand
Joined: Aug 06, 2007
Posts: 486
|
|
As far as i know, Instance member is called the Class member. Correct me if i am wrong..
|
 |
swapnil vedpathak
Greenhorn
Joined: Mar 15, 2006
Posts: 8
|
|
class variables are not bound to any instance.but instance varibles are bound to object. class Temp{ int i; static int j; } in above case, i- instance varible j- class varible
|
 |
Gayathri Sugavanam
Greenhorn
Joined: Apr 03, 2008
Posts: 4
|
|
hi friends... I have a doubt with method-local inner class. I understand that the method-local inner class object doesn't have access to the local variables of the method the inner class is in. This is because the local variables live on the stack and their life is over once the method completes.. But the inner class object may still be alive even after the method completes. Hence the object doesn't have access to the local variables. But if they are marked as final, the object is able to access it. HOW..? Do the final variables live on the heap like the object? Kindly explain and thanks in advance for any reply.
|
 |
 |
|
|
subject: inner class
|
|
|