How to make sure an object returned will not be modified in Java

hugos

In Java you can't return const references to objects as you can in C++. How can I return a reference to an object that will be able to be modified inside a class but not from outside?

Consider this example:

I have class A with an object from class Slave. The object will be returned and used outside of the class A. Here's a code, I hope the comments help understand the problem.

public class TestReference
{
    public static void main(String[] args) {
        TestReference tr = new TestReference();
        TestReference.A a = tr.new A();

        Slave slave = a.getConstSlave();
        slave.printName();

        a.setSlaveName("New Name");
        slave.printName(); // also changes, this is a reference to the object inside class A

        slave.setName("Fake name"); //this shouldn't be able to happen, we are making modifications outside class A
        slave.printName();
    }

    private class A {
        private Slave slave;
        public A() {
            this.slave = new Slave("A");
        }
        public Slave getConstSlave() {
             // How to make sure this slave will not be modified outside
             // and will keep consistency with this object modifications
             // inside the class A
            return slave;
        }
        public void setSlaveName(String name) {
            slave.setName(name);
        }
    }

    private class Slave {
        private String name;
        public Slave(String name) {
            this.name = name;
        }
        public void printName() {
            System.out.println(name);
        }
        public void setName(String name) {
            this.name = name;
        }
    }
}
D3xter

IMO there is no way to do that in java, like you would do it in C++.

What you could do is create an Interface (e.g. ReadOnlySlave) and return that instead of Slave:

public interface ReadOnlySlave {
    public void printname();
}

public class Slave : ReadOnlySlave {
   public void printname() { ... }

   public void setName() { .... }
}

public ReadOnlySlave getConstSlave() {
   return slave;
}

Then you are able to modify the slave inside your class and the change is visible everywhere, but outside they are not able to change anything.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to make sure an object returned will not be modified in Java

From Dev

How to make sure an object will really be moved from?

From Dev

Weak requirements: how to make sure an object is initialized

From Dev

Weak requirements: how to make sure an object is initialized

From Dev

How to make sure my map is never modified once set in my Builder pattern?

From Dev

How to make sure that a method is used after an object is created in golang?

From Dev

how to make sure mocked object is called only once in mockito

From Dev

How to make sure your object is zero-initialized?

From Dev

How to make sure resource is released, with nested object hierarchy?

From Dev

how to make sure client uses proxy object in proxy design pattern?

From Dev

How do I make sure my partition returned to my primary OS after uninstalling Ubuntu

From Dev

How to make sure that a unique record is returned to each concurrent request from a asp.net controller action method

From Dev

how to split a javascript returned String object in java

From Dev

How Do I Make Sure Only One JRadioButton Is Selected In Java?

From Dev

How to make sure that Collections in java hold only one Type?

From Dev

Android Volley: how to make json request without returned object?

From Dev

Not sure why I am getting [object Object] returned

From Dev

Not sure how to create an object with Java Scanner and storing it inside an ArrayList

From Dev

How to return modified timezone Date object in java.

From Dev

Which type to declare to avoid copy and make sure returned value is moved

From Dev

How to make object immutable in java

From Dev

how to make sure nginx is running

From Dev

How to make sure certificate is not changed?

From Dev

How to make sure AsyncTask is executed

From Dev

How to make sure that firewall is off?

From Dev

how to make sure nginx is running

From Dev

How to make sure call is asynchronous?

From Dev

How to make sure there are no excessive commas

From Dev

(Java) Varying which object is modified

Related Related

  1. 1

    How to make sure an object returned will not be modified in Java

  2. 2

    How to make sure an object will really be moved from?

  3. 3

    Weak requirements: how to make sure an object is initialized

  4. 4

    Weak requirements: how to make sure an object is initialized

  5. 5

    How to make sure my map is never modified once set in my Builder pattern?

  6. 6

    How to make sure that a method is used after an object is created in golang?

  7. 7

    how to make sure mocked object is called only once in mockito

  8. 8

    How to make sure your object is zero-initialized?

  9. 9

    How to make sure resource is released, with nested object hierarchy?

  10. 10

    how to make sure client uses proxy object in proxy design pattern?

  11. 11

    How do I make sure my partition returned to my primary OS after uninstalling Ubuntu

  12. 12

    How to make sure that a unique record is returned to each concurrent request from a asp.net controller action method

  13. 13

    how to split a javascript returned String object in java

  14. 14

    How Do I Make Sure Only One JRadioButton Is Selected In Java?

  15. 15

    How to make sure that Collections in java hold only one Type?

  16. 16

    Android Volley: how to make json request without returned object?

  17. 17

    Not sure why I am getting [object Object] returned

  18. 18

    Not sure how to create an object with Java Scanner and storing it inside an ArrayList

  19. 19

    How to return modified timezone Date object in java.

  20. 20

    Which type to declare to avoid copy and make sure returned value is moved

  21. 21

    How to make object immutable in java

  22. 22

    how to make sure nginx is running

  23. 23

    How to make sure certificate is not changed?

  24. 24

    How to make sure AsyncTask is executed

  25. 25

    How to make sure that firewall is off?

  26. 26

    how to make sure nginx is running

  27. 27

    How to make sure call is asynchronous?

  28. 28

    How to make sure there are no excessive commas

  29. 29

    (Java) Varying which object is modified

HotTag

Archive