aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt on package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt on package" Watch "Doubt on package" New topic
Author

Doubt on package

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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?


Thanks<br />Dinesh
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
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

Joined: Aug 06, 2007
Posts: 486
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

Joined: Apr 26, 2005
Posts: 516
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

Joined: Aug 16, 2005
Posts: 14269
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
From my understanding you are supposed to import the class before using it .
Instead of using "extends MyClass.method"
I dont think this works.


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
yong huang
Greenhorn

Joined: Jul 10, 2007
Posts: 7
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

Joined: Aug 06, 2007
Posts: 486
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

Joined: Apr 26, 2005
Posts: 516
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

Joined: Aug 06, 2007
Posts: 486
I am sorry.
I will take liitle efforts to understand the thing but it will take soemtime.

Anyways Thanks for explaining.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on package