1

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.

3 Answers 3

4

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.

2
  • You mean to say after calling the method getString(); it is upto the programmer to store that returned String? Mar 18, 2012 at 5:54
  • 1
    @R122: Yes. For methods like getString() that don't do anything other than return a value, you will probably want to store the return value (otherwise, the call would be useless). For methods like append() that change things, there is frequently no reason to store the returned value value (unless you actually want it).
    – SLaks
    Mar 18, 2012 at 5:55
3

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
0
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.