0

Can anyone please explain why instance variable 'root' is null?

public class Test{
    String root;

    public void assignRoot(){
        baseAssignment(root);
    }

    public void baseAssignment(String root){
        if(root == null){
            System.out.println("root:"+root);
            root = "assigned!";
            return;
        }

    }

    public String toString(){
        return root;
    }

    public static void main(String args[]){
        Test t = new Test();
        t.assignRoot();
        System.out.println(t);
    }
}

My output is:

root:null
null

But i am expecting

root:null
assigned!
riteshtch
  • 8,629
  • 4
  • 25
  • 38
Pradeep
  • 419
  • 5
  • 14
  • Try renaming the parameter name from root. Magically see it work ;) – OneCricketeer May 26 '16 at 11:45
  • I don't think this is a pass-by-reference vs pass-by-value problem. It is a problem of scope. –  May 26 '16 at 12:05
  • @cricket_007 The goal is not to just get things working but also to tech why something does or does not work. – cyroxis May 26 '16 at 14:16
  • @LutzHorn Fixing the scope problem is only one way to fix that problem, but it would result in ugly code. Understanding why OPs current approach doesn't work (and it is not because of scopes) is what is needed here. – Tom May 26 '16 at 14:26

6 Answers6

4

There is no need to pass instance variables for method calls within the class:

public class Test{
    String root;

    public void assignRoot(){
        baseAssignment();
    }

    public void baseAssignment(){
        if(root == null){
            System.out.println("root:"+root);
            root = "assigned!";
            return;
        }

    }

    public String toString(){
        return root;
    }

    public static void main(String args[]){
        Test t = new Test();
        t.assignRoot();
        System.out.println(t);
    }
}

Output:

$ javac Test.java 
$ java Test
root:null
assigned!

or you can use this.root="assigned!" to refer the instance variable and not the local parameter.

riteshtch
  • 8,629
  • 4
  • 25
  • 38
1

You're just assigning a value to your local parameter root in your method, but not the class attribute root. Rename the parameter or use the this-keyword in front of it and it should work. A good explanation, why it behaves like that, can be found here.

Community
  • 1
  • 1
Dominic B.
  • 1,897
  • 1
  • 16
  • 31
  • "global variable" doesn't belong in java world; I guess you mean class attribute? – Preuk May 26 '16 at 11:56
  • @Preuk Yes, thanks. I'm really not the Java-guy, but I'm programming in C# and therefore I used this name. I edited it. – Dominic B. May 26 '16 at 11:58
1

Because in baseAssignment() method you didn't initialize field root, you just change the local parameter root.

Instead of:

root = "assigned!";

you should write:

this.root = "assigned!";
0
    if(root == null){
        System.out.println("root:"+root);
        this.root = "assigned!";

You should use this.root as you only change the value of the String given as parameter named root too.

Preuk
  • 632
  • 7
  • 18
0

That's my method parameters should always be final. Making the parameter root final would cause a compile error on

root = "assigned!";

You must assign to this.root. Change your code to:

public void baseAssignment(final String root){
    if(root == null){
        System.out.println("root:"+root);
        this.root = "assigned!";
        return;
    }

}
  • @Jack The question was closed as a duplicate more than an hour after I've posted my answer. Please don't try to be smart. –  May 31 '16 at 12:44
  • You haven't try to search that question before answering that question it's not a good practice first try to search related question if you don't find any than you have to give answer , by the way i am not smart like you. – J.D. May 31 '16 at 12:50
  • @Jack I see. You are reading the "How to answer" manual, look up my answers, and point out any fault you find. Well, have fun. –  May 31 '16 at 12:51
0

Use any of the one approach..

public void baseAssignment(String root){
    if(root == null){
        System.out.println("root:"+root);
      // root = "assigned!";
         this.root = "assigned!";
        return;
    }

}

or

public void baseAssignment(String root1){
    if(root1 == null){
        System.out.println("root:"+root);
        root = "assigned!";
        return;
    }

}

How it is explanation

Class MyClass{
String root="";
 public void baseAssignment(String root){
  root = "local variable"; // pointing to method local variable root 
  System.out.println("root:"+root);
  this.root = "class variable"; // pointing to class variable root 
  System.out.println("root:"+this.root);   
  }
 }

Output of this:

root:local variable
root:class variable
Sumon Mal
  • 71
  • 4
  • It would be helpful to explain why instead of just posting code so the author can learn. – cyroxis May 26 '16 at 12:10
  • when ever we are considering any variable with same name in this case root, inside method / block always all the operations performed on the local variable, to point the outer variable need to be explicitly mention with "this" as that'd be a class variable. please check the commented line on the 3rd block of my answer :) – Sumon Mal May 26 '16 at 14:12