rcode
rcode

Reputation: 49

Change and use variable by different methods

I've got a small question because oft a topic I didn't understand. There is one variable in a class. In the first method I want to give her a value. The second method have to change the value of this variable again. The new value of the variable is needed by a third method. I want to change and use this variable on every point of the class. Is this possible? I hope you know what I mean. Thanks for every help!

Upvotes: 2

Views: 831

Answers (3)

Frank
Frank

Reputation: 881

You can do , here an example :

public class PassingV {

    private int i;


    public int getI() {
        return i;
    }


    public void setI(int i) {
        this.i = i;
    }


    public PassingV firsM(PassingV a){
        a.setI(1);
        return a;

    }

    public PassingV secondM(PassingV a){
        a.setI(2);
        return a;

    }

    public PassingV thirdM(PassingV a){
        a.setI(3);
        return a;

    }


    @Override
    public String toString() {
        return "PassingV [i=" + i + "]";
    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub

        PassingV v = new PassingV();

        System.out.println(v.firsM(v).toString());
        System.out.println(v.secondM(v).toString());
        System.out.println(v.thirdM(v).toString());

    }

}

Result:

enter image description here

Becarful to the types of objects you are using and becarful at the methods (accessors for example ) you define ,or not define in the class .

They can totally change the way how your object has seen from the outside .

Lets modifiy our class a bit and lets see what happen .

Now instead of int i will use a String parameter.

public class PassingV {

    private String i;



    public String getI() {
        return i;
    }

    public void setI(String i) {
        this.i = i;
    }

    public PassingV firsM(PassingV a){
        a.setI("HEY ");
        //substring but it return the original value :D
        System.out.println(a.getI().substring(2));
        return a;

    }

    public PassingV secondM(PassingV a){
        a.setI("JOE ");
        return a;

    }

    public PassingV thirdM(PassingV a){
        a.setI("LETS GO");
        return a;

    }





    @Override
    public String toString() {
        return this.getI() ;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        PassingV v = new PassingV();

        System.out.println(v.firsM(v).toString());
        System.out.println(v.secondM(v).toString());
        System.out.println(v.thirdM(v).toString());

    }

}

Result:

enter image description here

As you can see with String object something changed , it happen because is Immutable object

Following this link you can read more about Immutable Objects

Upvotes: 0

dvelopp
dvelopp

Reputation: 4315

If I understand you correctly, you need to send a variable into the methods so that they can modify it. As I understand, here it could be difficult becuause if you use wrapper types, they can't be modified. In such a case you can create a class that wraps your variable and can change it's values or you can use ready-to-go solutions from third party libraries.

For example, in apache-comons, they have a package:

org.apache.commons.lang3.mutable

That contains mutable wrappers for all primitive types(e.g. MutableInt). Using your own wrapper or this classes you can modify variable inside methods and keep result saved without returning new values from these methods.

Upvotes: 0

Salih Erikci
Salih Erikci

Reputation: 5087

It is possible.

public class Test{
  int counter;

  public void initCounter(int initValue){
    counter = initValue;
  }

  public void incCounter(){
    counter++;
  }
  public void decCounter(){
    counter--;
  }
  public void printCounter(){
    System.out.println(counter);
  }
}

Upvotes: 2

Related Questions