The Artima Developer Community
Sponsored Link

Java Answers Forum
Newbie:how to get a reference out of a method

1 reply on 1 page. Most recent reply: Jan 1, 2003 12:00 PM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Andres Corral

Posts: 3
Nickname: iampiti
Registered: Jan, 2003

Newbie:how to get a reference out of a method Posted: Jan 1, 2003 10:22 AM
Reply to this message Reply
Advertisement
Hi!
I'm a newbie with Java and i have a question:

I have a piece of code like this:

...
Person j=null;
...

public static boolean foo(Person p){

....
b=new Person();
if (...){
p=b;
return true;
}
else return false;
}

as you can imagine i want to get the boolean value and the reference to the newly created object in p if the condition is true. Everything seems to go fine inside the method but after calling foo(j), j still points to null. Can you help?

Thanks!


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Newbie:how to get a reference out of a method Posted: Jan 1, 2003 12:00 PM
Reply to this message Reply
This is not possible in Java. Everything (method parameters) in Java is passed by value. When you are passing j to foo() then j is passed by value and when you call foo() then p has a copy of j, which in your case will be null. By copy here I meant the copy of the reference and not the copy of whole object. If you assign a new object reference to p then it is assigned to p only and j still refers to null. So, you cannot make j refer to another object inside foo(). If you want to still return a boolean from foo() then you can set the value of p in some instance variable and get it as:
if ( foo() ) {
j = get the Value of p here which you created in foo();
}

Flat View: This topic has 1 reply on 1 page
Topic: Post Method Previous Topic   Next Topic Topic: Reading flat files from java which has no delimiters

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use