public class A
{
static private int x=100;
static final int Z=10;
public static void main(String [] args)
{
x=10;
// Z=x;
System.out.println(Z+" "+x);
}
}//为什么private的变量可以改变值呢,不是说private也只隐含的final吗?
{
static private int x=100;
static final int Z=10;
public static void main(String [] args)
{
x=10;
// Z=x;
System.out.println(Z+" "+x);
}
}//为什么private的变量可以改变值呢,不是说private也只隐含的final吗?
所以 private 方法直接就被默认是final 因为final可以提高效率(可以省略后期联编了)
final这词的意思自己字典,如果用他修饰变量,表示变量的值不能改变,一般有final的变量声明的时候就要赋值,当然了你也可以声明好了在static块中赋值.因为final变量不能改变值,所以一般final变量都用个static,这样能让他每个类都只有一个变量,节约空间好像也是书上说的.
如果用他修饰方法,表示这个方法不可以被继承,书上好像说这样子的方法运行起来比较快但限制了可扩展性,我也没试过.
access a private method, you can't override it. ...
任何private方法都是隐性final的。因为你不能访问一个private方法,所以你不能覆盖它。TIJ 第6章 还指出:This issue can cause confusion, because if you try to override a private
method (which is implicitly final) it seems to work, and the compiler
doesn’t give an error message...
这个问题会产生迷惑,因为如果你试图去覆盖一个private方法(它是隐性final的),它似乎也工作,编译器也不给出错误消息。"Overriding" can only occur if something is part of the base-class
interface. ... If a method is private, it isn't part of the base-class interface. It
is just some code that's hidden away inside the class, and it just happens
to have that name, ... You haven't overridden the method, you've just created a new
method.
“覆盖”只会在有什么是基类接口的一部分时才发生。……如果一个方法是private的,它就不是基类接口的一部分,它只是隐藏在类里面的一些代码罢了,而碰巧也有那个名字。……你没有覆盖那个方法,你只是创建了一个新的方法。
私有变量在类的内部是可以改变值的
----------
1,private修饰变量时,代表私有变量,并无默认final的含义,在类内部可以改变值。
2,private修饰方法时,代表私有方法,默认含有final的意义,不可覆盖。
----------
楼主的问题属于第一种情况。关于private的这些知识,自己也不清楚,借着这个问题,打些汉字,加深印象:)
怎么说也只是指"被private修饰的方法"....
注意是方法而不是属性,代码中被private修饰的属性可从来没人说过隐含final修饰的...就像竹子说的,谁这么说就杀了他...而且从Java的理论上也不应该这么理解,我猜测这是编译器自己优化的...
就像"ab"+"cd"会被直接优化成"abcd"一样...也许编译的时候编译器把private的方法都加上了final修饰...期待强人去测试一下看看...我机器抽了,环境用不了呜喵~~~~`