Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Pass By Reference

  Asked By: Steven    Date: Jan 11    Category: Java    Views: 884
  

Please look at the code below :

public class Target {

private String target = null;

public void function( ) {
System.out.println( target );
_function( target );
System.out.println( target );
}

public void _function( String target ) {
target = "showForm";
}

public static void main( String ar[ ] ) {
Target t = new Target( );
t.function( );
}

}



When the above code is executed the output is
null
null

The output is not
null
showForm

This is due to the fact, that while calling _function( ) in function( )
actually a copy of target is sent, not the reference of the original object.
( Am I correct ? )

Is there any method to send the reference of the original target object. So
that output woud come as
null
showForm

I hope, I am able to express my problem..... Any Clue on this..

Share: 

 

5 Answers Found

 
Answer #1    Answered By: Shelia Wells     Answered On: Jan 11

try a method  that returns a String instead of changing the String:

public String _function() {
return "showForm";
}

and

target = _function()

 
Answer #2    Answered By: Roop Kapoor     Answered On: Jan 11

Change the code  to:

public void  _function( String target  ) {
this.target = "showForm";
}

I guess that you want to change the value of your private  member and
not the value of the parameter that you pass  over to this function.

 
Answer #3    Answered By: Abasi Massri     Answered On: Jan 11

_function() has a local parameter named "target" that hides
your instance member "target". You'll either need to change the
parameter name OR use "this.target" to set the instance member.

Also, be careful attempting side-effects like this. It can make the
code much more difficult to maintain.

 
Answer #4    Answered By: Aylin Kaya     Answered On: Jan 11

In Java there are two general types of data: primitive types
(int, double, char, etc.) and objects.
Objects are really pointers, and because string  is a class,
any string in Java is a pointer.

Every variable in Java is passed by value, never by reference.
When you pass  a primitive type, this concept is clear, but
when you pass a class, this concept is not so clear.

So I will explain this thing "What happens when you pass an
object as a parameter of a function?"
- The pointer is passed by value, so you can't change the pointer,
assigning things like: c = new MyClass(...) or c = "my string",
- But you can change its content, accesing its attributes or
its methods.

Here is an example of what you can do:

*** MyClass.java
public class  MyClass {
private int code;
private String name;

public MyClass (int acode, String aname) {
setCode (acode);
setName (aname);
}
public void  setCode (int acode) { code  = acode; }
public int getCode () { return code; }
public void setName (String aname) { name = aname; }
public String getName () { return name; }
}

*** MyApp.java
public class MyApp {
public static  void anyMethod (MyClass p) {
p.setCode (101);
p.setName ("Any other name");
}

public static void main  (String[] args) {
MyClass c = new MyClass (1,"Any name");
System.out.println ("code=" + c.getCode() + ", name=" + c.getName());
anyMethod (c);
System.out.println ("code=" + c.getCode() + ", name=" + c.getName());
}
}

 
Answer #5    Answered By: Rhys Evans     Answered On: Jan 11

java always pass  reference (pointer) of objects to functions
when you pass string  to function, the function  receives pointer of the
string
but when you assign new value in function body to function argument, it only
affect the local pointer, not the original  object

consider this in C:
void f1(char * s) {
s = "1234"; // you don't modify the original "hello" string

// but
strcpy(s, "1234"); // you modify the original "hello" string
}
void main() {
char * s = "hello";
f1(s);
}

in java:
target = "showForm";
is equal to : target  = new String("showForm"); // you assign new object
reference, not modifying original object

you can solve your problem  with:

public String _function( String target ) {
return "showForm";
}

//or if you need more than one outputs
public Object[] _function( String target ) {
return new Object[] { "showForm", "str2", "str3" };
}

public void  _function( StringBuffer target ) {
// cannot use String as JVM treat String like basic type
target.setLength(0);
target.append("showForm");
}

 
Didn't find what you were looking for? Find more on Pass By Reference Or get search suggestion and latest updates.




Tagged: