Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Package & modifier question

 
luco zhao
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
Now I have these 3 files located in some dir just as comments describe, why compile error occurs?
//File 1: c:/abc/def/Q.java:
package def;
public class Q {
protected int protectedVar;
}
//File 2: c:/abc/Tester.java:
import def.Q;
public class Tester extends Q {
Q q = new Q();
Sub sub = new Sub();
public void someMethod() {
q.protectedVar = 3; // compiler error,why?
protectedVar = 7; // fine

Tester t = new Tester();
t.protectedVar = 11; // fine
sub.protectedVar = 15; // fine
}
}
//File 3: c:/abc/Sub.java:
public class Sub extends Tester {
}
Thanks in advance!
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi luco,
Your error has nothing to do with file placement. It is your misunderstanding of the protected modifier. According to the JVM a protected variable can be accessed only if one of the following is true:
1. From within the class that declared it
2. From within any subclass of the class that declared it.
Lets look at your compiler error case:
Is q inside the braces of the class Q? It doesnt' look like it to me. This means that number 1 above is false.
Is q extending the class Q? It doesn't look like it to me because it is just an instance of the Q class. This means that number 2 above is false.
No more cases to check ... no trues so far ... result = compiler error!
We can even go further and see why your 2 fines work.
protectedVar = 7; // fine
works because number 2 above is true. Why? Because the class Tester extends Q.
t.protectedVar = 11; // fine
sub.protectedVar = 15; // fine
both work because number 2 above is true. Why? Because t is of type Tester and we already know that Tester extends Q. Sub is a subclass of Tester so it falls in the same logic as t.
Regards,
Manfred.
[ April 25, 2002: Message edited by: Manfred Leonhardt ]
 
luco zhao
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manfred, Thank you so much.
Now I think I know the ropes after ur kind explanation.
But I've got some supplement.
According to the JVM a protected variable can be accessed only if one of the following is true:
1. From within the class that declared it
2. From within any subclass of the class that declared it.
3. From the same package, right?
As far as my question is concerned, Q.class is not in the same package as Tester.class, so compile error occurs. If we put both of them in the same dir (import and package clause let out, of course), complier will not complain anymore.
In this circumstance, Number 3 is Okay.
Great thanks,
luco zhao
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic