• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object reference

 
Nagaraju Mulinti
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are at least three reasons why the code you posted will not compile:

  • getvalues is not the same as getValues - Java is case-sensitive
  • You did not specify a return type for the getValues method
  • The 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.
     
    Campbell Ritchie
    Sheriff
    Posts: 48652
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Have you been reading the other threads in "beginning Java"? Somebody else has a similar problem at the moment.
     
    Roldan Baldo
    Ranch Hand
    Posts: 99
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 537
    Eclipse IDE Java Python
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic