Results 1 to 3 of 3
  1. #1
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default returning objects explanation please

    Java Code:
    // Returning an object.
    class Test {
    int a;
    Test(int i) {
    a = i;
    }
    Test incrByTen() {
    Test temp = new Test(a+10);
    return temp;
    }
    }
    class RetOb {
    public static void main(String args[]) {
    Test ob1 = new Test(2);
    Test ob2;
    ob2 = ob1.incrByTen();
    System.out.println("ob1.a: " + ob1.a);
    System.out.println("ob2.a: " + ob2.a);
    ob2 = ob2.incrByTen();
    System.out.println("ob2.a after second increase: "
    + ob2.a);
    }
    }
    Output:
    Java Code:
    ob1.a: 2
    ob2.a: 12
    ob2.a after second increase: 22
    Im confuse with this code snippet:
    Java Code:
    Test incrByTen() {
    Test temp = new Test(a+10);
    return temp;
    }
    I wonder if this is a method? but it type is like the name of the constructor..
    Can anyone Explain this to me? I found this on book but i can't understand the explanation.

  2. #2
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: returning objects explanation please

    Test incrByTen() //is a method which returns a Test object...

    Test temp = new Test(a+10) // constructs a new Test object.. and that object is referrenced by a variable called temp
    and lastly temp is returned
    Last edited by Kagiso; 06-22-2013 at 01:46 PM.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: returning objects explanation please

    I wonder if this is a method? but it type is like the name of the constructor..
    Can anyone Explain this to me? I found this on book but i can't understand the explanation.
    You don't though - constructor names are always the same as the name of the class, and they are always uppercase. incrByTen() doesn't fit either of those criteria.

    Kagiso is correct - incrByTen() is just an ordinary method that returns a Test object as a return type. It might be more obvious if you include your method scope (which is generally a good idea anyway):

    Java Code:
    class Test {
        int a;
    
        Test(int i) {
            a = i;
        }
    
        public Test incrByTen() {
            Test temp = new Test(a + 10);
            return temp;
        }
    }

Similar Threads

  1. Returning local objects
    By Lowest0ne in forum New To Java
    Replies: 1
    Last Post: 12-11-2012, 01:14 AM
  2. Function call returning array of objects
    By ShitalJain in forum New To Java
    Replies: 4
    Last Post: 06-25-2011, 09:39 AM
  3. Polygons... returning and passing objects?
    By asherwolf in forum New To Java
    Replies: 3
    Last Post: 07-09-2010, 04:41 PM
  4. need explanation
    By marie in forum New To Java
    Replies: 2
    Last Post: 03-21-2010, 03:35 PM
  5. need a little explanation
    By cew27 in forum New To Java
    Replies: 7
    Last Post: 12-13-2007, 11:39 PM

Posting Permissions

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