Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: immutable

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default immutable

    How to create immutable class without using final keyword?


  2. #2
    Junior Member
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: immutable

    final does not mean the class is immutable, you just can't reassign the variable to another object instance.

    Example:

    final MyObject obj = new MyObject();
     
    // obj = new MyObject();
    // you can't reassign it
     
    obj.setField(true);
    // but you can modify it

    If you want to create an immutable class, don't make any public mutators (methods which modify the object's fields).
    Last edited by GabrielNegut; May 7th, 2012 at 08:13 AM.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: immutable

    Quote Originally Posted by GabrielNegut View Post
    final does not mean the class is immutable, you just can't reassign the variable to another object instance.
    Final means different things in difference contexts See Writing Final Classes and Methods (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
    To quote:
    Quote Originally Posted by Oracle
    A class that is declared final cannot be subclassed. This is particularly useful, for example, when creating an immutable class like the String class
    Marking a class as final does not by default mean the class is immutable. Encapsulating fields and not providing setters is a step in the right direction, but depending upon what the fields are, one may also need to have some way to deep copy fields which can potentially be modified by clients when accessed by getters.

Similar Threads

  1. To Make class immutable which has ref to other mutable objects
    By tcstcs in forum Object Oriented Programming
    Replies: 1
    Last Post: May 4th, 2012, 10:42 AM
  2. immutable
    By saurabhRBMI in forum Java Theory & Questions
    Replies: 2
    Last Post: October 1st, 2011, 11:23 AM
  3. Replies: 3
    Last Post: July 24th, 2011, 06:13 AM