开发者

Invisible Objects in java

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 t开发者_JS百科his and how can I avoid it in the future?


I suspect you're under the impression that this:

setName = new Stack(setLength);

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:

Object foo = null;
METHOD_NAME(foo, 5);

then foo will still be null afterwards.

I suggest you return the value from your method instead. For example:

public static Stack METHOD_NAME(Object setName, int setLength){
    while(true) {
        try {
            return new Stack(setLength);
        } catch (InstantiationException e){
            System.err.println(e.getMessage());
            SET_NUM(1);
        }
    }
}

Note the return instead of breaking to a label, and while(true) which I find more readable than for (; ;).


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).


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.


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 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:

public static Object METHOD_NAME(int setLength) {
    while (true) {
        try {
            return new Stack(setLength);
        } catch (InstantiationException e){
            System.err.println(e.getMessage());
            SET_NUM(1);
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜