Results 1 to 8 of 8
  1. #1
    saiganesh44 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Reference of an object

    Can anyone explain, why the output is that way? I mean, if the argument is just a reference of the object which is passed, why isn't the 'i' value changed ? which did in case of 'k' value

    Java Code:
    public static void change(int j) {
    		j = 20;
    	}
    
    	public static void change(info in){
    		in.setK(20);
    	}
    	
    	public class info {
    		int k;
    
    		public void setK(int k) {
    			this.k = k;
    		}
    
    		public int getK() {
    			return k;
    		}
    	}
    
    	public static void main(String args[]) {
    		int i = 10;
    		System.out.println("initial value of i is " + i);
    		change(i);
    		System.out.println("after value of i " + i);
    		info infObj = new ObjectReference().new info();
    		infObj.setK(i);
    		System.out.println("initial " + infObj.getK());
    		change(infObj);
    		System.out.println("after " + infObj.getK());
    	}

    OUTPUT :
    initial value of i is 10
    after value of i 10
    initial 10
    after 20

  2. #2
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Reference of an object

    I'm going to give you a horribly vague answer because I don't know 'proper' terminology to use. In your usage, the parameter you are passing is not really a reference because primitive types like int are not objects. Therefore, you cannot change the value of 'i" as seen by main() by passing it as a parameter to change().

  3. #3
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Reference of an object

    This should help you understand better. Scroll down to the "Passing Primitive Data Type Arguments" section of the Passing Information to a Method or a Constructor topic of the Classes and Objects Tutorial.

  4. #4
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    40
    Rep Power
    0

    Default Re: Reference of an object

    It is necessary to use ObjectReference() constructor ?

  5. #5
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Reference of an object

    Quote Originally Posted by kosmos890 View Post
    It is necessary to use ObjectReference() constructor ?
    Please don't hijack the thread. Start a new one or even do a search, I'm sure there are a hundred variations of that basic question. Search on "constructor".

  6. #6
    saiganesh44 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: Reference of an object

    Quote Originally Posted by jlczuk View Post
    This should help you understand better. Scroll down to the "Passing Primitive Data Type Arguments" section of the Passing Information to a Method or a Constructor topic of the Classes and Objects Tutorial.
    hmmmmmmm, now it get the difference between primitive data type and reference data type, it is confusing though. Thanx

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

    Default Re: Reference of an object

    You can find a very clear (and entertaining!) article here: JavaRanch Campfire - Cup Size: a Story About Variables

    After you've read and digested it, go on to part two linked from the end of part one.

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

  8. #8
    saiganesh44 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: Reference of an object

    Quote Originally Posted by DarrylBurke View Post
    You can find a very clear (and entertaining!) article here: JavaRanch Campfire - Cup Size: a Story About Variables

    After you've read and digested it, go on to part two linked from the end of part one.
    That part two link best explained my question, Thnx :)

Similar Threads

  1. What to do with object reference?
    By kyle_maddisson in forum New To Java
    Replies: 6
    Last Post: 11-04-2011, 05:58 AM
  2. Reference object
    By clj89 in forum New To Java
    Replies: 5
    Last Post: 10-22-2011, 11:32 PM
  3. object and reference
    By aizen92 in forum New To Java
    Replies: 11
    Last Post: 04-01-2011, 08:39 PM
  4. Object and reference
    By katie in forum New To Java
    Replies: 2
    Last Post: 10-19-2009, 03:45 PM
  5. Getting the Object Reference Name
    By Deathmonger in forum New To Java
    Replies: 2
    Last Post: 03-12-2008, 02:51 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
  •