Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on package

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • 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.

Ans - C

Can't understand why the ans is C. Can someone expalin. The code looks fine
What is wrong?
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Did you even try to copy/paste it in your Java editor ? It would tell you the error right away.. I just feel you would learn more about these kind of things if you put some effort to find it yourself.

Look at this: System.out.println("x = " + x);
Tell me what you think might be the error here..

Regards,
Alex
[ March 02, 2008: Message edited by: Alex Belisle Turcot ]
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, i know that.
But i need to know why it is failing at line 2 as i have mentioned earlier.
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dinesh Tahiliani:
Yeah, i know that.
But i need to know why it is failing at line 2 as i have mentioned earlier.


Ok then...
At line 2 of Test2, only x is being used...
Look at how x is declared. Where can be x be accessed from ?
=> A and B are not in the same package..

bye,
Alex
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dinesh, when you copy a question from a book or mock exam, we require that you quote your sources. So, please tell us where you copied it from.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From my understanding you are supposed to import the class before using it .
Instead of using "extends MyClass.method"
I dont think this works.
 
yong huang
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dinesh,
It confuses me about the line number in the question. But the problem is in this line:
4. System.out.println(�x = � + x);
Cause the access modifier of static variable x is default level, which makes x only visible to other classes in the same package as class Test1.
Test1 can be inherited by Test2 cause the access modifier of public, which makes it universally visible.



Originally posted by Dinesh Tahiliani:
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.

Ans - C

Can't understand why the ans is C. Can someone expalin. The code looks fine
What is wrong?
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • 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. }

Hello,
I have now corrected the line.
Can anyone now expalin me why its failing.
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dinesh Tahiliani:
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. }

Hello,
I have now corrected the line.
Can anyone now expalin me why its failing.


What did you change exactly ? Did you just copy the line I put in bold, which I copied from you myself? You simply copied the same line over...

The point was x is declared with a package modifier.
Your classes Test1 and Test2 are not in the same package, so Test2 cannot see Test1.x.

One solution would be to declare x with a protected or public modifier.

I'm sorry to say, I find very frustrating the little effort you seem to put before posting. I'll let other posters answer you.

Alex
[ March 04, 2008: Message edited by: Alex Belisle Turcot ]
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry.
I will take liitle efforts to understand the thing but it will take soemtime.

Anyways Thanks for explaining.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic