Can one object access a private variable?
来源:互联网 发布:linux 内核启动参数 编辑:程序博客网 时间:2024/06/16 23:46
Q. Can one object access a private variable of another object of the same class?
A:
Yes! One object can access a private variable of another object of the same class. The private field can be accessed from static method of the class through an instance of the same class too.
Here is an example:
//****************
public class Test1 {
private int i;
Test1(int ii) {
i = ii;
System.out.println("Self: " + i);
// to avoid infinite recursion
if (i != 3) {
Test1 other = new Test1(3);
other.i++;
System.out.println("Other: " + other.i);
}
}
public static void main(String[] args) {
Test1 t = new Test1(5);
// The private field can be accessed from
// static method of the same class
// through an instance of the same class
System.out.println(t.i);
}
}
//****************
A:
Yes! One object can access a private variable of another object of the same class. The private field can be accessed from static method of the class through an instance of the same class too.
Here is an example:
//****************
public class Test1 {
private int i;
Test1(int ii) {
i = ii;
System.out.println("Self: " + i);
// to avoid infinite recursion
if (i != 3) {
Test1 other = new Test1(3);
other.i++;
System.out.println("Other: " + other.i);
}
}
public static void main(String[] args) {
Test1 t = new Test1(5);
// The private field can be accessed from
// static method of the same class
// through an instance of the same class
System.out.println(t.i);
}
}
//****************
- Can one object access a private variable?
- FPGA 关于 Only one always block can assign a given variable write_rdy
- java反射调用方法时,Class . can not access a member of class . with modifiers "private" 等异常解决
- Intellij IDEA取消access can be private提示
- a regression model that predicts one model variable.
- R can't be resolved to a variable解决办法
- How can I print a JavaScript object?
- ACCESS LOW/HIGH BYTES OF A INTEGER VARIABLE
- Selenium Firefox Python错误can't access dead object?
- Null pointer access: The variable number can only be null at this location。 错误解决
- Null pointer access: The variable condition can only be null at this location。
- Null pointer access: The variable number can only be null at this location。 错误解决
- Null pointer access: The variable sList can only be null at this location
- Null pointer access: The variable marketcommends can only be null at this location
- Warn:Null pointer access: The variable addressList can only be null at this location
- Null pointer access: The variable sb can only be null at this location
- Null pointer access: The variable temp can only be null at this location
- Null pointer access: The variable al can only be null at this location问题的解决
- 关于页码的几种设置方法
- “cookies”相关知识
- linux命令大全
- Vi 使用说明
- JavaScript中的Boolean,你了解多少?
- Can one object access a private variable?
- 限制类中产生对象的数目
- 单元测试利器 JUnit 4
- 系统启动没有声音,音量控制图标不能正常显示,但realplayer能正常播放
- 复制失效的JS代码
- linux errno code
- asp.net控件开发基础(10)
- 模式解决方案
- 通过XSL转换XML文件