Java returning an Object -
im learning java , im facing following error
object label1 = addlabel("first number"); and addlabel function
public object addlabel(string text) { jlabel label = new jlabel(text); add(label); return label; } i wondering why cant access of methods of label on variable label1 if im returining object?
ex : label1.setbounds(...);
you can access methods variable has available, , object has none of methods. why should not using object in way. yes, object variable holds jlabel, compiler knows variable can hold object of any type, , safe, allows object method calls.
possible solutions:
- you can cast:
((jlabel) label1).settext("foo"); - or better declare
label1jlabel variable , declareaddlabelreturn jlabel.
Comments
Post a Comment