aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please help 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 "Please help" Watch "Please help" New topic
Author

Please help

Li
Greenhorn

Joined: Jun 24, 2001
Posts: 1
Please help!!!
The following are Question from John Hunt mock exam:
Which variables can an inner class access from the class which encapsulates it?
A.All static variables
B.All final variables
C.All instance variables
D.Only final instance variables
E.Only final static variables
First time i select A and B, But the answer is A, B, C. Then i made a test program:
public class Test {
static int si = 5;
/*final*/ int i = 10;
static class Inner {
Inner() {
System.out.println("Access to static si = " + si);
// System.out.println("Access to no static / final i = " + i);
}
}
public static void main(String[] argc) {
new Inner();
}
}
The running result show that static inner class only access static variable in the class which encapsulates it. So the answer should be only A. Can anybody figure out if i am wrong? BTW, i use jdk1.3.


green horn
anand raman
Ranch Hand

Joined: Jun 06, 2001
Posts: 66
The answer should be A,B,C . The question mentions only "inner class". Why are u trying to create a static inner class. I guess a lot of confusion can be overcome if u just consider the following lines from the java tutorial

As a member of its enclosing class, a nested class has a special privilege: It has unlimited access to its enclosing class's members, even if they are declared private. However, this special privilege isn't really special at all. It is fully consistent with the meaning of private and the other access specifiers. The access specifiers restrict access to members for classes outside of the enclosing class. The nested class is inside of its enclosing class so that it has access to its enclosing class's members.

Treat static inner class as a static method. What all methods can this access?? Obviously static methods cant access non static variables.. SO the static classes cant access non static variables in the enclosing class.
Hope this clears ur doubt..
-Anand
Gokhan Gultekin
Greenhorn

Joined: Jun 23, 2001
Posts: 27
Hi Li,
If the question says "a static inner class" then only A is correct. Bcoz u cannot access to instance variables from a static inner class.U can only access to static variables of the outer class.
However if it is a non-static inner class then the correct anwser is A,B and C.
I reckon by saying "inner" class, Hunt is trying to say "non-static class".
Hope it helps.
Cheers, Gokhan
PS:Probably you will receive a warning due to name conventions in Javaranch!!.
Jason Li
Ranch Hand

Joined: Jun 24, 2001
Posts: 46
Hi, anand and Gokhan,
Thank you for your reply! I registered another name.
But I am still confused.
Does inner class only mean no static inner class? In my brain, i think inner class should include static and no-static inner class . And the question only mentions the inner class. So if i meet the question in exam, what answer should i select?

green horn
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Jason,
Thanks for re-registering with a proper name
There is no such thing as a 'static inner class'

JLS §8.1.2
An inner class is a nested class that is not explicitly or implicitly declared static

Technically a class enclosed within another class is a 'nested' class and if it is declared with the 'static' modifier it is a 'top-level nested class'.
Sun would not ask you a question on a 'static inner class'.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


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