wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Object creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object creation" Watch "Object creation" New topic
Author

Object creation

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Here you see a part of a program that creates different Valhold
objects; What's the difference?
public static void main (String args[])
v = new Valhold /Valhold created
v.another(v,i) /Call Valhold methode with Valhold-Object
/*-------------
v = new Valhold
another(v,i) / What does this?
System.out.println(v.i)
public void another (Valhold v, int i){
v.i = 10
}

------------------
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
sorry but i do not understand your code.
-- missing {}
-- v.i do declare a instance variable i somewhere?
etc..........
so if you can send a more complete code

------------------
Benjamin l�onard
evisor
www.evisor.com
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Here you see a part of a program that creates different Valhold
objects; What's the difference?
public static void main (String args[])
v = new Valhold /Valhold created
v.another(v,i) /Call Valhold methode with Valhold-Object
/*-------------
v = new Valhold
another(v,i) / What does this?
System.out.println(v.i)
public void another (Valhold v, int i){
v.i = 10
}
-----------
Given the following code what will be the output?
class ValHold{
public int i = 10;
}
public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
another(v,i);
System.out.println(v.i);
}//End of amethod
public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.println(v.i+ " "+i);
}//End of another
}
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
result will be :
10 0
20
you can see explanation at
http://www.javaranch.com/campfire/StoryPassBy.jsp
------------------
Benjamin l�onard
evisor
www.evisor.com
[This message has been edited by Benjamin Leonard (edited May 18, 2001).]
Laurent Lebrun
Greenhorn

Joined: May 15, 2001
Posts: 2
The output will be
10 0
20
Why ? Let's go through your program step by step.

Now amethod has a local variable i set to 99

Creation of a v object. In the constructor, it sets its instance variable i to 10
Thus v.i = 10

Surprisingly v.i = 30

Here the fun part begins ...
i will not be changed as the parameter is passed by value
The content of v can be changed but not the reference

v still points to the same object but the value of i may have changed (in fact, it will be changed to 20 ... see below)

Whatever the parameters may be, i = 0 and v.i = 20
The change of v.i will be seen by the calling method

A new ValHold object is created

The v reference is changed.
But it's only a copy of the reference is changed, this means that the reference in the "amethod" method is NOT changed

Thus, v.i = 10 (points to a newly created object)
i = 0 (first line of this method)

Laurent Lebrun
Evisor

[This message has been edited by Laurent Lebrun (edited May 21, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object creation