简体   繁体   English

Java:捕获未初始化的变量

[英]Java: Catch uninitialized variable

Suppose we have a case where a variable is declared in certain conditions: 假设在某些情况下声明了变量:

if (x = 1)
 boolean z = true;

Later on, we'd like to test if the variable z exists with try-catch. 稍后,我们要测试变量z是否存在try-catch。 Is this possible and if so, what exception should we catch? 这可能吗?如果可以,我们应该捕捉什么异常?

try {
 if (z)
  //do smth
} catch (<exception?> ex) {
 //do smth_else
}

Of course it would be possible to declare z before and change it's value accordingly in if block, but just hypothetically, is the above possible in Java? 当然,可以在if块中先声明z并相应地更改其值,但仅是假设,在Java中上述可能吗? Python for example has NameError that will be raised when accessed local or global variable is not declared. 例如,Python具有NameError,如果未声明访问的本地或全局变量,则会引发NameError。

Thanks! 谢谢!

HSI. 恒指。

What if you declared your variable like this: 如果您这样声明变量,该怎么办:

Boolean x = null;

In that case you could check for it being null or not. 在那种情况下,您可以检查它是否为null

An even better alternative would be using an enum to represent the uninitialized value: 一个更好的选择是使用一个枚举来表示未初始化的值:

public enum MyEnum {
    UNINITIALIZED, TRUE, FALSE;
}

because if you will try to maintain your code several months later you (or someone else) may be puzzled about the Boolean being null. 因为如果几个月后您将尝试维护代码,您(或其他人)可能会对Boolean为null感到困惑。

we'll get compilation error if the variable we are using is not declared or is not visible in current scope. 如果所使用的变量未声明或在当前作用域中不可见,则会出现编译错误。 If it is declared we can check for NullPointerException if that was object. 如果已声明,则可以检查NullPointerException是否为对象。 In case of primitive data types we should check for default values. 如果是原始数据类型,则应检查默认值。

Suppose we have a case where a variable is declared in certain conditions: 假设在某些情况下声明了变量:

Well it is difficult to assume because that would not compile: 很难假设,因为那不会编译:

  • you should use == to test for equality 您应该使用==来测试是否相等
  • you can't declare a variable in an if statement unless there is a block 除非有一个块,否则不能在if语句中声明变量

Now assuming you enclose that declaration inside a block, the scope of that variable would be that block and you wouldn't be able to use it in your try / catch block (unless it is inside the if block of course, but I don't think that's what you want). 现在假设您将该声明包含在一个块内,则该变量的范围将是该块,并且您将无法在try / catch块中使用它(除非它当然在if块内,但是我不会)认为那是您想要的)。

No, this is not possible in Java. 不,这在Java中是不可能的。 You have to have the variable declared before you can refer to it, otherwise you will get a compilation error. 您必须先声明该变量,然后才能引用它,否则会出现编译错误。

Boolean z = null;
if (x = 1){
  z = true;
}


if(z == null){
 //not initialized

}else{
  //initialized
}

1, first of all your syntax for java is wrong: it can be following 1,首先您对Java的语法是错误的:可以如下

if (x == 1)
  boolean z = true;

2, in Java You have to declared variable before use it. 2,在Java中,必须在使用变量之前声明它。

it's not possible, Java is a strongly typed programming language because every variable must be declared with a data type before it can be used. 不可能,Java是一种强类型的编程语言,因为在使用每个变量之前,必须先使用数据类型声明每个变量。

int x = 1;
boolean z = null;

if (x == 1)
 z = true;

try {
 if (z)
  //do smth
} catch (NullPointerException npe ) {
 //do smth_else
}

So far I understand , you wont be able to compile this piece of code. 到目前为止,我了解到,您将无法编译这段代码。 I can not remember any exception class but what I think is even if you "Invent" exception for this type of error. 我不记得任何异常类,但是即使您为这种类型的错误“发明”异常,我的想法也是。 It won't compile. 它不会编译。 Because default values of primitive types are assigned to uninitialized class variables called fields but for variable used in method body, it gives compile time error 因为原始类型的默认值分配给称为字段的未初始化类变量,但对于方法主体中使用的变量,它会产生编译时错误

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM