File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Object reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object reference" Watch "Object reference" New topic
Author

Object reference

Nagaraju Mulinti
Ranch Hand

Joined: Aug 10, 2008
Posts: 37
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

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.


    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39386
        
      28
    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: 537

    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Object reference