java中的不可见对象
以下代码是一个较大应用程序的一部分:
public static void METHOD_NAME(Object setName, int setLength){
tryLoop:
for( ; ; ){
try{
setName = new Stack(setLength);
break tryLoop;
}catch (InstantiationException e){
System.err.println(e.getMessage());
SET_NUM(1);
continue tryLoop;
}
}
}
每当我尝试使用在 try 块内初始化的堆栈对象时,都无法找到它,除非对它的引用位于 try 块内。这是为什么?以后如何避免呢?
The following code is part of a larger application:
public static void METHOD_NAME(Object setName, int setLength){
tryLoop:
for( ; ; ){
try{
setName = new Stack(setLength);
break tryLoop;
}catch (InstantiationException e){
System.err.println(e.getMessage());
SET_NUM(1);
continue tryLoop;
}
}
}
Whenever I try to use the stack object that was initialized within the try block, it cannot be found unless the reference to it is within the try block. Why is this and how can I avoid it in the future?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我怀疑您的印象是:这
将对调用者传入的参数产生一些影响。不会的。 Java 是严格按值传递的,无论该值是原始类型值还是引用。
换句话说,如果你这样做:
那么之后
foo
仍将是null
。我建议您从您的方法中返回该值。例如:
注意 return 而不是中断到标签,以及
while(true)
我发现它比for (; ;)
更具可读性。I suspect you're under the impression that this:
will have some impact on the argument passed in by the caller. It won't. Java is strictly pass-by-value, whether that value is a primitive type value or a reference.
In other words, if you do this:
then
foo
will still benull
afterwards.I suggest you return the value from your method instead. For example:
Note the return instead of breaking to a label, and
while(true)
which I find more readable thanfor (; ;)
.好吧,至少可以说,这个方法是相当非正统的 Java 代码。
此外,它似乎没有任何有意义的结果。它将自己的参数设置为一个新值(完全忽略原始值),并且从不返回任何内容或修改它传递的任何对象。
因此,除非 Stack 对象的构造具有某种从外部可见的效果,否则此方法不会执行任何有用的操作(在“无异常”情况下)。
Well, that method is ... pretty unorthodox Java code, to say at least.
Additionally it doesn't seem to have any meaningful result whatsoever. It sets its own parameter to a new value (entirely ignoring the original one) and never returns anything or modifies any object that it gets passed.
So unless the construction of a
Stack
object has some effect that is visible from the outside, this methods doesn't do anything useful (in the "no-exception" case).在 try 块之前声明一个方法代码变量,并将 setName 分配给它。然后将 new Stack() 分配给 try 块中的该变量,并在方法末尾返回它。
无论如何,修改参数的值通常都是不好的做法。
Declare a method scode variable before your try block and assign setName to it. Then assign new Stack() to that variable in your try block and return it at the end of your method.
Modifying the value of a parameter is usually bad practice anyways.
不知道你用标签做什么——继续;会工作得很好。其他事情在这里也有点可疑。在 try 块内声明的变量的作用域就是 try 块。当 Java 传递对象时,设置“setName”不会执行任何操作,并且更改引用以指向新对象不会影响传递的对象。至于无法在当前代码中使用 setName,您可以通过将其放在块之外,或者在 try 块内执行您需要的所有操作来避免它:) 您还可以返回它以允许调用者使用它。为什么要尝试捕获 InstantiationException?您最好检查 setLength 是否为有效大小,并让未捕获的异常验证 Java 本身的完整性。
No idea what you're using a label for -- continue; will work fine. Other things are a bit suspect here too. The scope for a variable declared inside a try block is just the try block. Setting 'setName' will do nothing as Java passes an object, and changing the reference to point to a new object will not affect the passed object. As for not being able to use setName in the current bit of code, you can avoid it by taking it outside of the block, or doing everything you need to inside the try block :) You can also return it to allow the caller to use it. Why are you trying to catch InstantiationException? You'd be better off checking that setLength is a valid size and let uncaught exceptions validate the integrity of Java itself.
Java 不支持引用传递,因此对
setName
的赋值不会将任何值传递回调用者。您的代码的明显重写如下:
Java does not support pass-by-reference, so the assignment to
setName
does not pass any value back to the caller.The obvious rewrite of your code is as follows: