Results 1 to 9 of 9
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default What can a method return?

    This is from book SAM Teachyourself JAVA in 21 days:

    A method can return a reference to an object, a primitive data type, or no value at all.
    My first question is: How can a method return a reference to an object ?
    My second question is: Can't methods also return objects?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: What can a method return?

    Java uses references to objects (pointers). The object itself is not passed like with a primitive.
    How can a method return a reference to an object ?
    with a return statement:
    AClass refToObject = new AClass(); // create a reference to an object
    return refToObject; // return a reference to object

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: What can a method return?

    Then the method would be something like:

    public AClass dosomething()
    { //.. return refToObject }

    Is it correct ?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: What can a method return?

    Do you have a compiler? You should write a small test program and compile it to see if the compiler thinks your code is ok.
    It looks ok to me.

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: What can a method return?

    Thanks.

    So I either return an actual object of the type or a reference to AClass, it does not matter.

    ( Still talking about: public AClass dosomething()
    { //.. return refToObject } )

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: What can a method return?

    References are passed. References are returned. Objects are neither passed nor returned.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: What can a method return?

    This is from JAVA API :

    forName
    public static Class<?> forName(String className)
    throws ClassNotFoundException
    Returns the Class object associated with the class or interface with the given string name.
    It says, it returns the Class object ?

    Is it actually returning a reference to a Class object then?
    Last edited by fatabass; 01-29-2012 at 02:19 PM. Reason: return -> returns

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: What can a method return?

    There is some ambiguity when talking about objects and references to objects. Most/all??? of the time you are working with a reference to an object.

  9. #9
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

Similar Threads

  1. Why is this method return 0
    By africanhacker in forum New To Java
    Replies: 7
    Last Post: 06-30-2011, 06:21 PM
  2. cant return value from a method
    By gedas in forum New To Java
    Replies: 2
    Last Post: 03-23-2011, 07:37 AM
  3. Not able to return the method value
    By dmakshay2002 in forum Advanced Java
    Replies: 11
    Last Post: 05-28-2010, 02:07 PM
  4. Method won't return value
    By footyvino in forum New To Java
    Replies: 2
    Last Post: 03-26-2010, 10:49 AM
  5. Return value of method
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 08:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •