Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Object mutability

Object mutability

Scheduled Pinned Locked Moved Java
questionfunctionaltutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Neo10101
    wrote on last edited by
    #1

    How would one mutate an object that is final? By referencing the object and then doing a mutate() operation on it? e.g.:

    final Object o = new Object();
    o.mutate();

    I don't understand. If it was declared final, then how come you are allowed to mutate it (or is that not true)? How would one make the Object immutable then? Meaning: not mutable in any way, not even by reference. How do I make sure a reference cannot modify the object it refers to? So that MyObject o = new Object(); o.modify(); is not possible? I also read on Wikipedia the following example:

    final Position pos = new Position(); //pos is immutable

    members: pos.x, pos.y, pos.z are mutable still, unless they are declared final. So if I am understanding this correctly, in order to make an object/class completely immutable, everything in it including its reference has to be declared final? e.g.:

    final Position pos = new Position();

    class Position {
    final int x;
    final int y;
    final int z;

    public Position(){
    x=1;
    y=2;
    z=3;
    }

    final void doA(){}
    final void doB(){}
    }

    L 1 Reply Last reply
    0
    • N Neo10101

      How would one mutate an object that is final? By referencing the object and then doing a mutate() operation on it? e.g.:

      final Object o = new Object();
      o.mutate();

      I don't understand. If it was declared final, then how come you are allowed to mutate it (or is that not true)? How would one make the Object immutable then? Meaning: not mutable in any way, not even by reference. How do I make sure a reference cannot modify the object it refers to? So that MyObject o = new Object(); o.modify(); is not possible? I also read on Wikipedia the following example:

      final Position pos = new Position(); //pos is immutable

      members: pos.x, pos.y, pos.z are mutable still, unless they are declared final. So if I am understanding this correctly, in order to make an object/class completely immutable, everything in it including its reference has to be declared final? e.g.:

      final Position pos = new Position();

      class Position {
      final int x;
      final int y;
      final int z;

      public Position(){
      x=1;
      y=2;
      z=3;
      }

      final void doA(){}
      final void doB(){}
      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      See Java theory and practice: To mutate or not to mutate?[^].

      Use the best guess

      N 1 Reply Last reply
      0
      • L Lost User

        See Java theory and practice: To mutate or not to mutate?[^].

        Use the best guess

        N Offline
        N Offline
        Neo10101
        wrote on last edited by
        #3

        Referring to that article, Listing 1: Q.: "Because Date is mutable, the scheduleTask method must be careful to defensively copy the date parameter (perhaps through clone()) into its internal data structure. Otherwise, task1 and task2 might both execute tomorrow, which is not what was desired. Worse, the internal data structure used by the task scheduler could become corrupt." A few questions on this: 1) What is 'copying defensively'? 2) What do they mean by 'internal data structure'? 3) How could task1 and task2 execute tomorrow if the code is to be executed today? Then how can new Date() be tomorrow? I don't understand. 4) How would the internal data structure become 'corrupt'? What is 'corrupt' in this context? Containing inconsistent values?

        L 1 Reply Last reply
        0
        • N Neo10101

          Referring to that article, Listing 1: Q.: "Because Date is mutable, the scheduleTask method must be careful to defensively copy the date parameter (perhaps through clone()) into its internal data structure. Otherwise, task1 and task2 might both execute tomorrow, which is not what was desired. Worse, the internal data structure used by the task scheduler could become corrupt." A few questions on this: 1) What is 'copying defensively'? 2) What do they mean by 'internal data structure'? 3) How could task1 and task2 execute tomorrow if the code is to be executed today? Then how can new Date() be tomorrow? I don't understand. 4) How would the internal data structure become 'corrupt'? What is 'corrupt' in this context? Containing inconsistent values?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          These questions should be addressed to the writer of that white paper.

          Use the best guess

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups