This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Package level question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Package level question." Watch "Package level question." New topic

Package level question.

Ram Han
Ranch Hand

Joined: Feb 26, 2002
Posts: 49
1. package test1;
2. public class Test1 {
3. static int x = 42;
4. }
1. package test2;
2. public class Test2 extends test1.Test1 {
3. public static void main(String[] args) {
4. System.out.println(�x = � + x);
5. }
6. }

What is the result?
A. x = 0
B. x = 42
C. Compilation fails because of an error in line 2 of class Test2.
D. Compilation fails because of an error in line 3 of class Test1.
E. Compilation fails because of an error in line 4 of class Test2.

Answer is C.

Is using "test1.Test1 " is not valid one ?

marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by MuraliRam Narasimhan:
... Is using "test1.Test1 " is not valid one ?

That part is okay. The problem is that the variable x has default ("package") access, so it's not available in test2.Test2. The answer should be E (confirmed by testing).
[ December 08, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
uzma Akbar
Ranch Hand

Joined: Sep 21, 2005
Posts: 40
I agree with Marc Weber. Though the variable is static but it has package level access so any subclass accessing in a different package will end up at compilor error.

If you are getting answer C in addition to E. The reason being your compilor being unable to locate the file. To rectify that copy the test1 folder in the test2 folder and then javac -classpath . test2/

This is one way to do it but there are different ways to include files or folders in class path.

I hope this will help

I agree. Here's the link:
subject: Package level question.
jQuery in Action, 3rd edition