1

This question is a common issue, and I have tried to look at some thread as Is Java "pass-by-reference" or "pass-by-value"? or How to change an attribute of a public variable from outside the class but in my case I need to modify a boolean variable, with a Singleton instance.

So far I have a class, and a method which changes the boolean paramter of the class. But I would like to separate this mehod in a manager. The scheme is something like:

public class Test{
    private boolean b;
    public String getb(){}
    public void setb(){}
    String test = ClassSingleton.getInstance().doSomething();
}

public class ClassSingleton{
    public String doSomething(){
        //here I need to change the value of 'b'
        //but it can be called from anyclass so I cant use the set method.
    }
}

Thanks, David.

Community
  • 1
  • 1
Dayerman
  • 3,973
  • 6
  • 38
  • 54
  • 1
    **Java is Call By Object Sharing** -- In Java this is normally referred to as "Pass By Value [Of The Reference]". Fit the problem *to* Java. That is not the Java-way :) –  May 31 '11 at 08:12

5 Answers5

1

If I understand your requirement - this can solve your problem:

public interface IUpdatable
{
    public void setB(boolean newValue);
}

public class Test implements IUpdatable
{
    private boolean b;
    public String getb(){}
    public void setB(boolean newValue) {this.b = newValue;}
}

public class ClassSingleton
{
    public String doSomething(IUpdatable updatable)
    {
        updatable.setB(true);
        ...
    }
}

This way the Singleton does not need to know your Test class - it just knows the interface IUpdatable that supports setting the value of B. Each class that needs to set its B field can implement the interface and the Singleton can update it and remain oblivious to its implementation.

RonK
  • 9,472
  • 8
  • 51
  • 87
1
  1. You could extract public void setb(){} into an interface (let's call it BSettable), make Test implement BSettable, and pass an argument of type BSettable into doSomething.

  2. Alternatively, you could make b into an AtomicBoolean and make doSomething accept (a reference to) an AtomicBoolean.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

Define b as static variable.

Then call Test.b = value

arthur86
  • 531
  • 1
  • 3
  • 20
  • Cant be declared as static as then anyclass can change the value and I want that each class have their own copy. – Dayerman May 31 '11 at 08:15
0

Perhaps:

public class Test {
   private static boolean b;
   public static String getB() {}
   public static void setB() {}
}

should help? Static fields and methods can be called without an instance (i.e. Test.setB();).

Miki
  • 7,052
  • 2
  • 29
  • 39
0

I think your question is not very clear and your sample code is really badly done. Do you actually mean something like this?

public class Test{
    private boolean b;
    public boolean getb(){return b;}
    public void setb(boolean b){this.b = b;}
    String test = ClassSingleton.getInstance().doSomething(this);
}

public class ClassSingleton{
    private static ClassSingleton __t__ = new ClassSingleton();
    private ClassSingleton() {} 
    public String doSomething(Test t){
        t.setb(true);
        return null;
    }

    public static ClassSingleton getInstance(){
        return __t__;
    }
}

Do you mean your manager is a singleton? or your test class should be singleton? Please be more specific

user658991
  • 566
  • 3
  • 7
  • So do you want to have the manager initialize the class when every instance is created? If so, you have to pass the instance to the manager. BUT consider the Factory pattern instead, which reduces the code dependency on each other. – user658991 May 31 '11 at 08:58