append() method of J2ME
Question
We know that in Java when a method returns a value we have to store that value in a variable of that type.
For example the getString() returns String and we store that value in a String variable.
In J2ME I was trying to create radio-buttons i.e. using the ChoiceGroup
class.
ChoiceGroup radio;
radio=new ChoiceGroup("Select your Color",Choice.EXCLUSIVE);
radio.append("Red",null);
radio.append("White",null);
radio.append("Green",null);
In book the signature of append() method is
int append(String string, Image img)
I want to ask that even though I am not storing the integer value returned from the append() method my code runs perfectly.
I am using Wireless toolkit 2.5.2
Note the book has not given any reasons for this and that's why I asked here.
Solution
We know that in Java when a method returns a value we have to store that value in a variable of that type.
Every part of that sentence is false.
You should get a better book.
OTHER TIPS
ChoiceGroup append
method returns the assigned index of the element. If you don't intend to use it, it's OK to ignore returned value.
Method signature - return value and parameters have meaning clearly defined in API documentation:
public int append(String stringPart,
Image imagePart)
Appends an element to the ChoiceGroup.
Specified by:
append in interface Choice
Parameters:
stringPart - the string part of the element to be added
imagePart - the image part of the element to be added,
or null if there is no image part
Returns:
the assigned index of the element
Throws:
NullPointerException - if stringPart is null
If you want to use the return value then you store in a variable or object.If you dont want then leave it .Its not a mistake in java