Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

package

 
xie li
Ranch Hand
Posts: 54
  • 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.
 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

Where do get so many question? Do let me know please
 
Shawnne James
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Vivek
 
xie li
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank u
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic