Interesting question from the prof.

When can you call a value returning method by itself ?

Page 1 of 1

4 Replies - 521 Views - Last Post: 30 September 2009 - 01:42 PM Rate Topic: -----

#1 ogmios  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 01-September 09

Interesting question from the prof.

Post icon  Posted 30 September 2009 - 07:52 AM

Thought-provoking question, at least for me, a newbie ;p

Quote

When is it ok to call a value returning method on a line by itself (not within another statement)?


I can't think of a time you would be able to do this, since you HAVE to do something with the value (I thought). I always thought you had to have a value returning method assigned to something or in an if statement or such... Can anyone shed some light on this ?

Is This A Good Question/Topic? 0
  • +

Replies To: Interesting question from the prof.

#2 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2670
  • View blog
  • Posts: 15,713
  • Joined: 24-September 08

Re: Interesting question from the prof.

Posted 30 September 2009 - 07:56 AM

Accessor methods within classes. ^___^
Was This Post Helpful? 0
  • +
  • -

#3 ogmios  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 01-September 09

Re: Interesting question from the prof.

Posted 30 September 2009 - 07:59 AM

What exactly does that do for you though ? Don't accessor methods just return an otherwise out of scope variable ?
Was This Post Helpful? 0
  • +
  • -

#4 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2670
  • View blog
  • Posts: 15,713
  • Joined: 24-September 08

Re: Interesting question from the prof.

Posted 30 September 2009 - 08:08 AM

No. When you have a constructor within a class, you catch the parameters with mutators. Then you would return them seperately with accessors. This way another class can jack around with your variables directly. Kinda a security issue.
Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1997
  • View blog
  • Posts: 4,149
  • Joined: 11-December 07

Re: Interesting question from the prof.

Posted 30 September 2009 - 01:42 PM

Some methods return a value to indicate how a certain operation went.

For example, the List remove(Object o) method removes an object from a list. If it found the object and removed it true is returned. If the object wasn't in the list at all, false is returned.

Sometimes this can be useful but other times you don't care about the return value. In those cases just call the method without assigning the return value to a method.

http://java.sun.com/...va.lang.Object)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1