Results 1 to 8 of 8
Thread: Reference of an object
- 04-20-2012, 08:09 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 8
- Rep Power
- 0
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
- 04-20-2012, 08:26 PM #2
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
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().
- 04-20-2012, 08:34 PM #3
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
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.
- 04-20-2012, 09:01 PM #4
Member
- Join Date
- Apr 2012
- Posts
- 24
- Rep Power
- 0
Re: Reference of an object
It is necessary to use ObjectReference() constructor ?
- 04-20-2012, 09:09 PM #5
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
- 04-21-2012, 06:42 AM #6
Member
- Join Date
- Oct 2011
- Posts
- 8
- Rep Power
- 0
- 04-21-2012, 10:50 AM #7
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-21-2012, 11:22 AM #8
Member
- Join Date
- Oct 2011
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
What to do with object reference?
By kyle_maddisson in forum New To JavaReplies: 6Last Post: 11-04-2011, 05:58 AM -
Reference object
By clj89 in forum New To JavaReplies: 5Last Post: 10-22-2011, 11:32 PM -
object and reference
By aizen92 in forum New To JavaReplies: 11Last Post: 04-01-2011, 08:39 PM -
Object and reference
By katie in forum New To JavaReplies: 2Last Post: 10-19-2009, 03:45 PM -
Getting the Object Reference Name
By Deathmonger in forum New To JavaReplies: 2Last Post: 03-12-2008, 02:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks