我正在读一本关于Java的书,它说你可以把全班宣布为final
.我想不出任何我会用它的地方.
我刚接触编程,我想知道程序员是否真的在他们的程序中使用它.如果他们这样做,他们何时使用它,以便我能更好地理解它并知道何时使用它.
如果Java是面向对象的,并且你声明了一个类final
,那么它是否会阻止具有对象特征的类的想法?
在Java中,我们使用final
带变量的关键字来指定其值不被更改.但我发现你可以改变类的构造函数/方法中的值.同样,如果变量是,static
那么它是编译错误.
这是代码:
import java.util.ArrayList;
import java.util.List;
class Test {
private final List foo;
public Test()
{
foo = new ArrayList();
foo.add("foo"); // Modification-1
}
public static void main(String[] args)
{
Test t = new Test();
t.foo.add("bar"); // Modification-2
System.out.println("print - " + t.foo);
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常,没有错误.
现在将变量更改为static
:
private static final List foo;
Run Code Online (Sandbox Code Playgroud)
现在是编译错误.这final
真的有用吗?
我有一个带有private static final
字段的类,不幸的是,我需要在运行时更改.
使用反射我得到这个错误: java.lang.IllegalAccessException: Can not set static final boolean field
有没有办法改变价值?
Field hack = WarpTransform2D.class.getDeclaredField("USE_HACK");
hack.setAccessible(true);
hack.set(null, true);
Run Code Online (Sandbox Code Playgroud) 我不明白其中final
的关键字是真的方便的时候它是在方法的参数使用.
如果我们排除使用匿名类,可读性和意图声明,那么对我来说似乎几乎一文不值.
强制某些数据保持不变并不像看起来那么强大.
如果参数是基元,那么它将没有任何效果,因为参数作为值传递给方法,并且更改它将在范围之外没有任何影响.
如果我们通过引用传递参数,那么引用本身就是一个局部变量,如果从方法中更改引用,那么从方法范围外部不会产生任何影响.
考虑下面的简单测试示例.尽管该方法改变了给定的参考值,但该测试仍然通过,但它没有效果.
public void testNullify() {
Collection<Integer> c = new ArrayList<Integer>();
nullify(c);
assertNotNull(c);
final Collection<Integer> c1 = c;
assertTrue(c1.equals(c));
change(c);
assertTrue(c1.equals(c));
}
private void change(Collection<Integer> c) {
c = new ArrayList<Integer>();
}
public void nullify(Collection<?> t) {
t = null;
}
Run Code Online (Sandbox Code Playgroud) 在Java中,我们看到许多final
可以使用关键字的地方,但它的使用并不常见.
例如:
String str = "abc";
System.out.println(str);
Run Code Online (Sandbox Code Playgroud)
在上述情况下,str
可以final
但通常不会这样做.
当一个方法永远不会被覆盖时,我们可以使用final关键字.类似地,如果一个类不会被继承.
在任何或所有这些案例中使用final关键字是否真的能提高性能?如果是这样,那怎么样?请解释.如果正确使用final
真正重要的性能,应在Java程序员开发什么习惯,使关键字的最好用?
我在Java 8中玩lambdas,我遇到了警告local variables referenced from a lambda expression must be final or effectively final
.我知道当我在匿名类中使用变量时,它们必须在外部类中是最终的,但仍然 - 最终和有效最终之间有什么区别?
在Java中,有什么区别:
private final static int NUMBER = 10;
Run Code Online (Sandbox Code Playgroud)
和
private final int NUMBER = 10;
Run Code Online (Sandbox Code Playgroud)
两者都是private
和final
,不同之处在于static
属性.
什么更好?为什么?
编辑:我需要更改几个变量的值,因为它们通过计时器运行几次.我需要通过计时器每次迭代不断更新值.我无法将值设置为final,因为这会阻止我更新值,但是我收到了我在下面的初始问题中描述的错误:
我以前写过以下内容:
我收到错误"不能引用在不同方法中定义的内部类中的非final变量".
这种情况发生在双重调用价格和价格调用priceObject上.你知道我为什么会遇到这个问题.我不明白为什么我需要最后的声明.此外,如果你能看到我想要做的是什么,我该怎么做才能解决这个问题.
public static void main(String args[]) {
int period = 2000;
int delay = 2000;
double lastPrice = 0;
Price priceObject = new Price();
double price = 0;
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
price = priceObject.getNextPrice(lastPrice);
System.out.println();
lastPrice = price;
}
}, delay, period);
}
Run Code Online (Sandbox Code Playgroud) 在Java中,静态最终变量是常量,惯例是它们应该是大写的.但是,我已经看到大多数人声称记录器是小写的,这在PMD中是违规的.
例如:
private static final Logger logger = Logger.getLogger(MyClass.class);
Run Code Online (Sandbox Code Playgroud)
我们应该使用LOGGER吗?
我有一个关于Java中的字符串的简单问题.以下简单代码段仅连接两个字符串,然后将它们与之进行比较==
.
String str1="str";
String str2="ing";
String concat=str1+str2;
System.out.println(concat=="string");
Run Code Online (Sandbox Code Playgroud)
比较表达式concat=="string"
返回false
那么明显(I明白之间的差值equals()
和==
).
当这两个字符串被声明时final
,
final String str1="str";
final String str2="ing";
String concat=str1+str2;
System.out.println(concat=="string");
Run Code Online (Sandbox Code Playgroud)
比较表达式concat=="string"
,在这种情况下返回true
.为什么会final
有所作为?它是否必须与实习生池做某事或我只是被误导?
final ×10
java ×10
static ×3
private ×2
attributes ×1
declaration ×1
java-8 ×1
lambda ×1
logging ×1
methods ×1
reflection ×1
string ×1