简体   繁体   English

我可以用另一个final定义一个final变量吗?

[英]Can I define a final variable with another final?

This is more of a hypothetical question but if I have some final called A and another final B that are both ints, I can't do this: 这更多是一个假设性的问题,但是如果我有一些都称为int的final A和另一个final B,我将无法做到这一点:

private final int A = B/2, B = (some kind of other derived number);

I am just wondering why. 我只是想知道为什么。 Any help would be awesome. 任何帮助都是极好的。 NetBeans popped up an error on this and I just want to know why it is a problem. NetBeans对此弹出了一个错误,我只想知道为什么会出现问题。

PS-The error that popped up said "illegal forward reference". PS-弹出的错误表示“非法前向参考”。

You are accessing variable B before you declare it. 在声明变量B之前,您正在访问它。 That's the reason for "illegal forward reference". 这就是"illegal forward reference".的原因"illegal forward reference".

Define variable B before A A之前定义变量B

private final int B = (some kind of other derived number), A = B/2;

Pretend you're the compiler: 假设您是编译器:

private final int ok. private final int还可以。 Mr user wants a "const" int 用户先生想要一个“ const”整数

A the variable is called A A变量称为A

= ...here comes the value = ...价值来了

B/2 HUH? B/2嗯? WHAT THE HELL IS B? 地狱是什么? NO ONE TOLD ME ANYTHING ABOUT B. STUFF YOU USER. 关于B.用户的问题,没人告诉我。 I'M OUT OF HERE... 我不在这里...

The existing answers don't answer the underlying question: 现有答案无法回答基本问题:

Why can I use methods that are defined later in my source file, but does the same with variables cause a forward reference error message? 为什么我可以使用稍后在源文件中定义的方法,但是对变量使用相同的方法会导致前向引用错误消息吗?

The answer is found in the JLS, more specifically JLS $12.4.1 答案可以在JLS中找到,更具体地说,是JLS $ 12.4.1

The static initializers and class variable initializers are executed in textual order , and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope ( §8.3.2.3 ). 静态初始值设定项和类变量初始值设定项以文本顺序执行 ,并且不能引用在类中声明的类变量,这些类的声明在使用后以文本形式出现,即使这些类变量在范围内(第8.3.2.3节 )。 This restriction is designed to detect, at compile time, most circular or otherwise malformed initializations. 此限制旨在在编译时检测大多数循环或格式错误的初始化。

This question was answered here. 在这里回答了这个问题。 Basically it means that you are trying to use a variable that is not initiated. 基本上,这意味着您正在尝试使用未初始化的变量。

Initialize B first, then use it to initialize A 首先初始化B,然后使用它初始化A

private final int B = ?, A = B/2;

illegal forward reference in java Java中的非法前向引用

Your code isn't failing because A and B are final. 您的代码不会失败,因为A和B是最终的。 It's failing because B isn't declared/initialized yet. 之所以失败,是因为尚未声明/初始化B。 If you declare it first, you'll be able to use them just fine. 如果先声明,则可以使用它们。

For example, 例如,

private final int C = 5;
private final int B = C/3;
private final int A = B/2;

This is fine because B is declared first :) 这很好,因为首先声明了B :)

"final" just means that you can't change the variable. “最终”仅表示您无法更改变量。 So something like this won't work 因此,像这样将无法正常工作

private final static int C = 5;
private final static int B = C/3;
private final static int A = B/2;

public static void main (String[] args) {
    A = 5;
}

because now we're trying to modify A which is "final" 因为现在我们正在尝试修改“最终”的A

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以用其他一些最终的String []来定义一个最终的String [] []吗? - Can i define an final String[][] with some other final String[]s? 如何模拟局部最终变量 - How can I mock a local final variable 如何增加最终的 integer 变量? - How can I increase the final integer variable? 无法定义私有静态final变量,因为它会抛出异常 - Can't define a private static final variable because it throws an exception 我可以在ejb 3.1单例中定义非最终静态字段吗 - Can I define a non final static field in ejb 3.1 singleton 定义对公共最终实例变量的期望 - Define expectations for public final instance variable 强制子类定义受保护的最终实例变量 - Forcing a subclass to define a protected final instance variable 如何解决错误:“我在封闭 scope 中定义的局部变量必须是最终的或有效的最终”? - How can I work around the error: “Local variable i defined in an enclosing scope must be final or effectively final”? 用另一个最终静态变量初始化最终静态变量时的内存消耗 - Memory consumption on Initializing final static variable with another final static 如何访问线程 lambda 中的非最终变量? - How can I access a non-final variable in a Thread lambda?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM