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 label1 jlabel variable , declare addlabel return jlabel.

Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -