Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Package level question.

 
Ram Han
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
uzma Akbar
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/Test2.java

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

Uzma
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic