• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objects by value/reference

 
vuthlarhi donald
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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:
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic