• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Package & modifier question

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic