Invisible Objects in java -
the following code part of 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 try use stack object initialized within try block, cannot found unless reference within try block. why , how can avoid in future?
i suspect you're under impression this:
setname = new stack(setlength);
will have impact on argument passed in caller. won't. java strictly pass-by-value, whether value primitive type value or reference.
in other words, if this:
object foo = null; method_name(foo, 5);
then foo
still null
afterwards.
i suggest return value method instead. 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 return instead of breaking label, , while(true)
find more readable for (; ;)
.
Comments
Post a Comment