问题
In the class 'Tosee' below, hiddenInt is visible when I call s.hiddenInt. However, when I create a "ToSee" object in another class, 'CantSee', the private variable isn't visible. Why is this so? I was under the impression that private means that in any instance of a class, the client cant see that particular instance variable or method? Why then am I able to see hiddenInt in the main method of 'ToSee'?
public class ToSee {
private int hiddenInt = 5;
public static void main(String[] args) {
ToSee s = new ToSee();
System.out.println(s.hiddenInt);
}
}
public class CantSee {
public static void main(String[] args) {
ToSee s = new ToSee();
System.out.println(s.hiddenInt);
}
}
回答1:
Private in Java means the variable or method is only accessible within the class where it is declared. If your impression about private was true, it will not be accessible anywhere ever which makes it completely useless.
回答2:
A main
method has special connotations in Java, yet it's still a method belonging to a particular class.
Private fields in the enclosing class are accessible to the main
method, either through a local instance (in the case of instance fields) or directly (in the case of static
fields).
回答3:
The modifier private makes a variable or method private to the type (class) it is declared in. So only this class can see it.
You can see the variable hiddenInt
in ToSee.main
because ToSee.main
is a static method of the TooSee
class. Thus it can access all private variables of a ToSee
, either static or instance variables.
Private does also NOT mean private to an instance. An instance of one class can access the private variables of another instance of the same class.
public class ToSee {
private int hiddenInt = 5;
public void printInt(ToSee toSee){
System.out.println(toSee.hiddenInt);
}
public static void main(String[] args) {
ToSee tooSee1 = new ToSee();
ToSee tooSee2 = new ToSee();
tooSee2.hiddenInt = 10;
tooSee1.printInt(tooSee2); // will output 10
}
}
回答4:
I was under the impression that private means that in any instance of a class,
the client cant see that particular instance variable or method?
Incorrect! Private access modifier simply means that the variable on which it is used will be accessible only in the enclosing class. Period. Since your main() method is in ToSee class which is where you have the hiddenInt private instance variable, it is visible. Where as in case of CantSee class which is not in the ToSee class it is not visible(you need to use getter/setter methods.)
回答5:
private
means invisible to any code outside of the outermost enclosing class it is present in. Since the CantSee
class is separate from the ToSee
class it cannot see the private field. If CantSee
and ToSee
were both members of the same class, or one was a member of the other, then you would be able to read the private field. A few examples of structures in which the private field is readable follow :
public class Outer {
public class ToSee {
...
}
public class CantSee {
...
}
}
or
public class CantSee {
...
public class ToSee {
...
}
}
来源:https://stackoverflow.com/questions/18917265/meaning-of-the-private-visibility-modifier