0

When I call up a method from the other class, it returns null. I'll just show you my code below

class One{

   Two two;

   oneMethod(){
      String str = "";

      two.twoMethod(str);
      S.o.p("Str in One class is " + str);



   }


}


class Two{
   String twoMethod(String str){
      str = "From Two class";

      S.o.p("Str in Two class is " + str);

      return str;

   }

}

The output looks like: Str in One class is ___ // meaning null

Str in Two class is From Two class

My understanding about this is, after calling twoMethod(), the str with which is initially null will now be overriden with the new str value "From Two class". Now when we print str in One class, it should be the same with what is printed in the Two class. That's how I understand the flow.

Did I miss any concept / rule about passing Strings? Any feedback is appreciated. Thanks!

Jong Onin
  • 215
  • 1
  • 5
  • 12
  • 1
    you´re using `str` as if java would work with `pass-by-reference`, that´s why i marked is as dupe. You need to reassing it in `One` as of `str = two.twoMethod(str);` in order for `str` to get a value from the `return`. That beeing said, you `str` parameter in `twoMethod` will be/ is useless there as the provided `String` will be reassigned nevertheless. – SomeJavaGuy Mar 15 '17 at 08:09
  • You should read materials about "immutable objects" in Java. You indeed miss a concept. Oracle have a small piece of documentation on https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html (here in the context of concurrency, because it's the most important topic for immutable objects). – Wis Mar 15 '17 at 08:13
  • 2
    Also this statement is not correct "Str in One class is ___ // meaning null". Variable str is the empty string. That is quite different than been null. – RubioRic Mar 15 '17 at 08:13

3 Answers3

1

You should write like this :

class One{
  Two two;
  oneMethod(){
  String str = "";

  str = two.twoMethod(str);
  S.o.p("Str in One class is " + str);
  }
}

for class one.

0

The problem, I see, is not that the method returns null. You have not initialized your class Two and you should understand whether java pass-by-reference or pass-by-value. You should initialize your class before you call one of its methods.

class One{

Two two;

oneMethod(){
  String str = "";
  Two two = new Two();
  str = two.twoMethod(str);
  S.o.p("Str in One class is " + str);
 }
}


class Two{
 String twoMethod(String str){
  str = "From Two class";
  S.o.p("Str in Two class is " + str);
  return str;
 }
}

In addition to the object initialization, please consider the comments under your question (String is immutable and Java pass-by-value).

Ad Infinitum
  • 3,560
  • 27
  • 33
0

Java passes references to objects by value. Thus, in the below code segment, str does not pass in twoMethod(String) by reference and that String object stays as it initialized (empty string).

oneMethod() {
    String str = "";
    two.twoMethod(str);
    System.out.println("Str in One class is " + str);
}
Dorukhan Arslan
  • 2,676
  • 2
  • 24
  • 42