File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question regarind Inner Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question regarind Inner Classes" Watch "Question regarind Inner Classes" New topic
Author

Question regarind Inner Classes

zulfiqar raza
Ranch Hand

Joined: Oct 18, 2000
Posts: 81
Hi folks,
I have two questions regarding inner classes.
1. I understand that a Non-Static Inner Class (Member class), cannot have static members as it cannot exist without an outer instance hence it cannot provide service such as static members.
But why can't static local classes define static members? What is the underlying logic that tells us that static local classes can't define static members. When they can infact exist without an outer instance!
2. Non-static inner classes (Member classes) or Local classes cannot have static members however they can extend classes having static members. In this case won't these static variables be inherited in the member classes / local classes. Then these member classes/ local classes would have existing static members? or??
Can anyone discuss?
thanks,
Zulfiqar Raza
P SOLAIAPPAN
Ranch Hand

Joined: Oct 20, 2000
Posts: 68
Hi,
1.Can you define a static variable inside a method(both static & non-static) ?
It is not possible. Like that you cannot have static varibles defined in side local classes which are inside a method.
2. Why non-static inner class connot have static mebers ?
Because the static member is always belongs to class not to the instance. Here the non-static inner class itself belong to an "instance" of outer class. Then how can you define a static variable inside.
But it is possible inside static inner class since this class does not tied with any instance.
I hope it is clear.
solaiappan
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Zulfiqar,
1. The logic seems to be that local inner classes are not members and the static modifier can only be applied to members.

... local inner classes are not members of the class...therefore they cannot be private, protected, public or static .. because these modifiers apply only to class members.
The Java Programming Language: Second Edtion pg 53

2. Local inner classes can inherit static members of the class they extend.

The above compiles and, when run, prints: Static var: 10. So local inner classes behave as you'd expect re: inheritance.
Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiousity.
-- Dorothy Parker


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question regarind Inner Classes