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.
Karu Raj
Ranch Hand
Joined: Aug 31, 2005
Posts: 479
posted
0
hi
Where do get so many question? Do let me know please
Shawnne James
Greenhorn
Joined: Oct 27, 2005
Posts: 13
posted
0
The answer is E. The member class variable x is declared using the default access modifier in class Test1 so it is visible only inside the package test1. Trying to access it in class Test2(found in package test2) will generate a compilation error.
Shawnne
Balaji Sampath
Ranch Hand
Joined: Sep 30, 2005
Posts: 63
posted
0
If the Class test1 is modified as follows: package test1; 2 public class Test1 { 3 public static int x = 42; 4 }
then the answer would be: choice: B.
Correct me if i am wrong.. regards Balaji.S
Naresh Gunda
Ranch Hand
Joined: Oct 15, 2005
Posts: 163
posted
0
If the Class test1 is modified as follows: package test1; 2 public class Test1 { 3 public static int x = 42; 4 }
then the answer would be: choice: B.
Yes, public members are accessible in every class, every package Regards Naresh
cathymala louis
Ranch Hand
Joined: Nov 02, 2005
Posts: 77
posted
0
Hi
Since the program has
static x = 42;
I think there should be an compiler error at line 4. So I think the option E is correct.
Please correct me if I am wrong.
vivekkumar sharma
Ranch Hand
Joined: Dec 21, 2005
Posts: 70
posted
0
Originally posted by Balaji Sampath: If the Class test1 is modified as follows: package test1; 2 public class Test1 { 3 public static int x = 42; 4 }
then the answer would be: choice: B.
Correct me if i am wrong.. regards Balaji.S
Hi Balaji,
even protected static int x = 42; will work, and i think it is better to make memebers protected if possible than to make public ,otherwise u will be un necessarily opening doors to ur class,which is not desierd