| Author |
Object reference
|
Nagaraju Mulinti
Ranch Hand
Joined: Aug 10, 2008
Posts: 36
|
|
Hi All,
What is the value will I get when I execute the below class.I guss, I will get null. Why don't we get "2" value. Can you please clarify me below process. Thanks in advance..!
public class MyClass{
public static void main(String a[])
{
String one=null;
String two=null:
getvalues(one,two);
System.out.println(two);
}
getValues(String one,String two)
{
two="2";
}
}
|
 |
Antany Vasanth
Ranch Hand
Joined: Jan 28, 2009
Posts: 43
|
|
Hi Nagaraj,
In both the "main" method and "getvalue" method the variable "one" and "two" are local variables. So the reference change for the variable "two" in the "getvalue" method doesn't reflect the variable "two" in the main method.
Hence the result is null.
Regards
Antany.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12956
|
|
There are at least three reasons why the code you posted will not compile:
getvalues is not the same as getValues - Java is case-sensitiveYou did not specify a return type for the getValues methodThe main() method is static, but getValues() is not - you cannot call a non-static method from a static one
Please make sure that the code example you post is correct. In this case the question was simple to understand, but making simple mistakes like that can confuse people who want to answer your question.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
|
Have you been reading the other threads in "beginning Java"? Somebody else has a similar problem at the moment.
|
 |
Roldan Baldo
Ranch Hand
Joined: Aug 11, 2009
Posts: 99
|
|
Nagaraju Mulinti wrote:Hi All,
What is the value will I get when I execute the below class.I guss, I will get null. Why don't we get "2" value. Can you please clarify me below process. Thanks in advance..!
google how methods work in java, and how to declare variables. i have an example for you.
i just made it closer to your code, for you to analyze it.
|
 |
Nitish Bangera
Ranch Hand
Joined: Jul 15, 2009
Posts: 536
|
|
Well with the above program with same getvalue of Roldan we can use to reassign two in the main method.
two = getvalue(one,two)
then print two. Also please make getvalue static otherwise you cannot use it in the main method without the instance.
|
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
|
 |
 |
|
|
subject: Object reference
|
|
|