• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Object reference

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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";

}

}



 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Marshal
    Posts: 76447
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Have you been reading the other threads in "beginning Java"? Somebody else has a similar problem at the moment.
     
    Ranch Hand
    Posts: 99
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.

     
    Ranch Hand
    Posts: 537
    Eclipse IDE Python Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    You know it is dark times when the trees riot. I think this tiny ad is their leader:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic