This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Objects by value/reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Objects by value/reference" Watch "Objects by value/reference" New topic
Author

Objects by value/reference

vuthlarhi donald
Ranch Hand

Joined: Jul 31, 2006
Posts: 76
public class Test7{
public Test7(){}
public Test7(Test7 ref){
this (ref,"Hai");
}
public Test7(Test7 ref,String str){
ref.Test7(str);
System.out.println("Hi");
}
public void Test7(String str){
System.out.println(str);
}
public static void main(String[] args){
Test7 t = new Test7();
Test7 t7 = new Test7(t);
}
}

can anyone explain to me the output of this programme in more details...I am having a problem when it comes to passing by value and pass by reference...
Sandip Sarkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 55
Your program is as follows:

public class Test7{
public Test7(){}//1
public Test7(Test7 ref){//2
this (ref,"Hai");//3
}
public Test7(Test7 ref,String str){ //4
ref.Test7(str);//5
System.out.println("Hi");//6
}
public void Test7(String str){//7
System.out.println(str);//8
}
public static void main(String[] args){
Test7 t = new Test7();//9
Test7 t7 = new Test7(t); //10
}
}

Now Line 10 calls for Line 2.
Line 3 passes it on to Line 4 .
Line 5 calls function at Line 7.
Line 8 prints "Hai".
Line 6 prints "Hi".

So the output is :
"Hai"
"Hi"

I hope it helped.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
[vuthlarhi donald:]   I am having a problem when it comes to passing by value and pass by reference...

What problem, your code does not demonstrate the passing by value v pass by reference design challenge efficiently:


"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14195
    
  20

Originally posted by vuthlarhi donald:
can anyone explain to me the output of this programme in more details...I am having a problem when it comes to passing by value and pass by reference...

In Java, everything is always passed by value. There is no pass by reference in Java.

See the JavaRanch Campfire Story: Pass-by-Value Please


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Objects by value/reference