Results 1 to 4 of 4
  1. #1
    maheshkanda's Avatar
    maheshkanda is offline Member
    Join Date
    Feb 2009
    Location
    Banglore
    Posts
    4
    Rep Power
    0

    Question What is an Immutable Class

    What is an Immutable Class :confused: will any one explain in details with example :)

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    Usually an immutable object/class is one which has all its fields declared as final, though more loosely, the term is sometimes used to mean any object that doesn't provide methods to change its state once instantiated (more strictly, you could use the term "unmodifiable" for this).

    The distinction is important in concurrent programming because if a field is declared final, then the JVM guarantees that it is safe to access that field from other threads without synchronization (as of Java 5).

  3. #3
    R O C K Y's Avatar
    R O C K Y is offline Member
    Join Date
    Feb 2009
    Posts
    19
    Rep Power
    0

    Default

    Immutable means not subject to change i.e static.
    eg:String objects are constants and immutable whereas String Buffer/String Builder(JDK 5.0) not , they are growable and modifiable objects.

    try this u will get the answer:
    String s = "abc"
    s.concat("123")
    s.o.p(s)

    StringBuffer sb = new StringBuffer()
    sb.append("abc123")
    s.o.p(sb.toString())

    1->abc
    2->abc123

    nw to correct first use
    s = s.concat("123") rather than s.concat("123") , it does not store anything.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Quick comment...

    Rocky... a small comment... since your posting in the "New to Java" sub-forum, it's not a good idea to abriviate code like "s.o.p" (which I assume means "System.out.println") since it could confuse the new posters to this forum.

    Thanks,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM
  2. Replies: 0
    Last Post: 05-13-2008, 06:16 AM
  3. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:12 PM
  4. Replies: 0
    Last Post: 04-02-2008, 07:04 AM
  5. Strings are immutable yet they can be changed ?
    By anjanesh in forum New To Java
    Replies: 4
    Last Post: 05-19-2007, 03:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •