Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Fubarable

Thread: immutable class

  1. #1
    yanivdan98 is offline Member
    Join Date
    Feb 2013
    Posts
    10
    Rep Power
    0

    Default immutable class

    hi

    if i have an immutable class and it has a mutable variables..

    Also, the class has a get method that return the mutable variable.

    is this class considered as immutable or mutable class?


    i will be glad for detailed explanation

    thanks alot

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: immutable class

    Quote Originally Posted by yanivdan98 View Post
    i will be glad for detailed explanation
    Sorry, but this looks as if you're posting a homework or interview question directly from the assignment and asking for a "detailed explanation" as well which many find her to be unethical.

    Rather than ask others to do your work, why not instead ask others to discuss it? What is your take on the answer to your question, and please support this answer as best you can.

  3. #3
    yanivdan98 is offline Member
    Join Date
    Feb 2013
    Posts
    10
    Rep Power
    0

    Default Re: immutable class

    hi

    it is not for my homework,

    honestly i have an exam and i want to be sure with its defenition

    i searched alot over google without answer for it....

    i just want to understand:

    when i return a mutable variable over the get method, it return it by value, meaning that its exposed to exterior changes?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: immutable class

    If you expose a mutable *reference* fields, then you risk changing the state of the immutable object. Your getter methods should return "defensive" copies of any reference fields.

    This and other components of an immutable class are all well spelled out in most Google searches such as this hit in my search: Java Practices -> Immutable objects.

    Best to read the tutorials that are available, and then come here to ask questions about points in the tutorials that confuse you, rather than ask us to re-write tutorials that are already easily available.
    yanivdan98 likes this.

Similar Threads

  1. mutable class extending into immutable class
    By willemien in forum New To Java
    Replies: 3
    Last Post: 09-13-2012, 09:26 PM
  2. How can I make this class immutable?
    By fatabass in forum New To Java
    Replies: 6
    Last Post: 12-18-2011, 10:08 PM
  3. how to write user defined class as Immutable?
    By srinivasmallabathula in forum New To Java
    Replies: 3
    Last Post: 07-04-2011, 11:50 PM
  4. What is Immutable in String
    By elektronika in forum New To Java
    Replies: 4
    Last Post: 12-10-2009, 12:58 PM
  5. What is an Immutable Class
    By maheshkanda in forum New To Java
    Replies: 3
    Last Post: 02-06-2009, 08:12 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
  •