2

I am trying to assign value to a class variable via a method. However, after the execution comes out of the scope of the method, the variable is still initialized to the default value. How do we accomplish this in Java?

I want to initialize x to 5 by calling the method hello(). I don't want to initialize by using a constructor, or using this. Is it possible?

public class Test {
    int x;
    public void hello(){
        hello(5,x);
    }
    private void hello(int i, int x2) {
        x2 = i;
    }
    public static void main(String args[]){
        Test test = new Test();
        test.hello();
        System.out.println(test.x);
    }
}
Andrei
  • 7,509
  • 7
  • 32
  • 63
psychorama
  • 323
  • 5
  • 17
  • You should read this: [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/q/40480) – Tom Jun 05 '15 at 15:25

3 Answers3

9

When you do

hello(5,x);

and then

private void hello(int i, int x2) {
    x2 = i;
}

it seems like you might be trying to pass the field itself as parameter to the hello method, and when doing x2 = i you meant x2 to refer to the field. This is not possible, since Java only supports pass-by-value. I.e. whenever you give a variable as argument to a method, the value it contains will be passed, not the variable itself.

(Thanks @Tom for pointing out this interpretation of the question in the comments.)

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • I don't want to use 'this' either. Is there still a way? – psychorama Jun 05 '15 at 14:48
  • @VisheshSrivastava Change the name of the variable instead. Your problem comes from the fact that your variable name shadows the attribute 's name. And btw, shadowing it is a bad practice. – Arnaud Denoyelle Jun 05 '15 at 14:49
  • Yes: remove the useless arguments of the method hello, which are completely ignored. Or at least, don't name arguments the same way as instance fields. – JB Nizet Jun 05 '15 at 14:49
  • @VisheshSrivastava Yes. Change the name of your input variable to something else, like `int in_x` or my favorite: `int x_` – NoseKnowsAll Jun 05 '15 at 14:49
  • @ArnaudDenoyelle Even if I cange the parameter name in the method as x2 instead of x and do x2=i, the class variable will still be initialized to 0. This won't work. – psychorama Jun 05 '15 at 14:51
  • I want to use the method's parameter to initialize the class variable. – psychorama Jun 05 '15 at 14:52
  • 1
    @VisheshSrivastava consider calling the method in your test. You're only calling the constructor. – JB Nizet Jun 05 '15 at 14:52
  • @VisheshSrivastava If you do x2 = i, it will obviously update `x2`, not `this.x`. But try to rename the parameter `x` to `x2` and call `x = i` in the method. Il will update `this.x`. – Arnaud Denoyelle Jun 05 '15 at 14:55
  • No, that is obvious that if I do, x = i, x will be made equal to i. What I want is, to make the value of x = i using x2. – psychorama Jun 05 '15 at 14:58
  • @VisheshSrivastava, answer updated. See the code snippet at the bottom of my answer. Please let me know if something is still unclear. – aioobe Jun 05 '15 at 15:14
  • Either I don't get the point or you're not telling him why his idea won't work. OP want an "assign" method which gets two variables and set the first argument to the value of the second one. Since Java doesn't support "pass-by-reference" this whole idea won't work. – Tom Jun 05 '15 at 15:29
  • @Tom, good catch. I didn't realize that that was what OP was attempting. Answer updated according to your interpretation. – aioobe Jun 05 '15 at 15:33
1

The class property x is only visible by using this.x in hello() because you have declared another variable called x in the method's arguments.

Either remove that argument:

private void hello(int i) {
    x = 5;
}

Rename the argument:

private void hello(int i, int y) {
    x = 5;
}

Or use this.x to set the class property:

private void hello(int i, int x) {
    this.x = 5;
}
Armand
  • 23,463
  • 20
  • 90
  • 119
0

You can create two methods.

public void setX(int a)//sets the value of x
{
    x=a;
}
public int getX()//return the value of x
{
return x;
}

call setX to set the value of x and getx to return the value x.

Essentially these are called getter and setter and we use them to access private members from outside the class.

aakansha
  • 695
  • 5
  • 18