2

Is it possible to pass the reference of my class attributes by reference? For example:

public class Myclass{
  private Boolean FRinCreation=false,
                  NFRinCreation=false, 
                  DSinCreation=false,
                  FRinEdit=false,
                  NFRinEdit=false,
                  DSinEdit=false;

  List<Boolean> getTabBools(ETargetReq req){
    switch (req){
      case ETR_FUNCTIONAL:
        return Arrays.asList(FRinCreation, FRinEdit);
      case ETR_NONFUNCTIONAL:
        return Arrays.asList(NFRinCreation, NFRinEdit);
      case ETR_DATASET:
        return Arrays.asList(DSinCreation, DSinEdit);
      case ETR_UNKNOWN:
        return null;
      default:
        return null;
    }
  }

  boolean otherFunction(){

    //Change the FRinCreation to true
    getTabBools(ETR_FUNCTIONAL).set(0, true);
  }

unfortunately this does not sot my class attribute value to true if i execute it. Can you guys help me out? I am a bit confused by javas pass by value.

ZeldaZach
  • 478
  • 1
  • 9
  • 18
Stefan x
  • 129
  • 1
  • 1
  • 7
  • 1
    Is `ETargetReq` an enum? – shmosel Jun 30 '17 at 03:33
  • Yes ETargetReq s a Enum. Sry should have mentioned this – Stefan x Jun 30 '17 at 03:34
  • 1
    Do you own its source? – shmosel Jun 30 '17 at 03:35
  • No it is an import – Stefan x Jun 30 '17 at 03:36
  • 1
    What's the idea of passing specific variables into the lists? Are you trying to restrict `otherFunction`'s access to the rest? Is `otherFunction` in the same class? Why not explicitly set `FRinCreation = true;`? – shmosel Jun 30 '17 at 03:41
  • The idea behind is that in the 'otherFunction' it is also possible that i need the booleans for another type of the enum. So i thought i make a method that will provide the needed booleans depending on the enum type – Stefan x Jun 30 '17 at 03:48
  • 1
    Also consider `EnumSet`, for [example](https://stackoverflow.com/a/6067501/230513). – trashgod Jun 30 '17 at 04:25
  • The issue isn't so much about "pass by value" versus "pass by reference" (which I think is slightly confusing terminology here), but that a `Boolean` is immutable. If you use some kind of mutable wrapper for a `boolean` (e.g. a `BooleanProperty`, though it might not be the best option), then you could do `getTabBools(ETR_FUNCTIONAL).get(0).set(true)` and achieve what you wanted. – James_D Jun 30 '17 at 17:27

1 Answers1

1

In Java, all objects are passed by reference other than primitive datatypes such as int, boolean, double.

What is meaning of pass by reference:

  1. When we create object variable then it is just variable pointing to some object in heap.
  2. If we change value of attributes of object then it will be reflected in heap stored object.
  3. That's the reason every variable reference pointing to that heap object value get's changed.

In your example FRinCreation variable is pointing to boolean object on heap.

Now after that you stored that objects in list and List is object holding list of object references. When you set value at index 0 then it will update reference at index 0 pointing to FRinCreation to something else.

That's the reason FRinCreation is still pointing to false.

Bottom line is : If you are expecting change in object value then change attributes inside the object and not object reference.

swapnil
  • 230
  • 3
  • 6