Sei sulla pagina 1di 14

Mutable and immutable classes

AGENDA
Introduction Mutable Classes Immutable Classes Cloning and Mutable Conclusion

Introduction
An Immutable object is a kind of object whose state cannot be modified after it is created where a Mutable Object can be modified after it is created.

Immutable
A class that contains methods (other than constructors) that change any of the data in an object of the class is called immutable classes and object of the class are called immutable objects.
Strings are immutable class.

In Java, objects are referred by references.


If an object is known to be immutable, the object reference can be shared.

For

example, Boolean, Byte, Character, Double, Float, Integer, Long, Short, and String are immutable classes in Java.

An immutable object is one whose externally visible state cannot change after it is instantiated. The String, Integer, and BigDecimal classes in the Java class library are examples of immutable objects -- they represent a single value that cannot change over the lifetime of the object.

Sample Program
Class Pro1 { public static void main(String[] args) { String str = "WELCOME"; System.out.println(str); str.toLowerCase(); //Doesnt impact on original content of Str System.out.println(str); }

Output
WELCOME

WELCOME

Modified Program
class Pro1

{
public static void main(String[] args) {

String str = WELCOME"; System.out.println(str); String str1 = str.toLower(); System.out.println(str1); }


}

Output

WELCOME
welcome

Mutable
A class that contains public methods or input methods that can change the data in an object of the class is called mutable classes and the object of the class are called mutable objects.
The class Date is an example of a mutable class. When defining any methods , that method should not return a reference to a mutable object. Instead use a copy constructor to return a reference to a completely independent copy of the mutable object.

A String Buffer is a string that can be changed. String Buffers are Mutable , theyre not inherently thread safe and thus many of the methods of String Buffer class are synchronized. The StringBuffer has the methods Append( )

Insert ( )

Mutable Objects in AWT and Swing


The java.awt package defines several classes that encapsulate geometric information.
AWT Geometry Classes
CLASS Point Dimension Insets Rectangle DESCRIPTION (x , y) location in space Component width and height Representation of the Borders of a container Area in a coordinate space

The java.awt.Component and java.awt.Container classes define methods to access certain geometric information.
public Point getLocation(); public void setLocation( Point loc);

public Dimension getSize();


public void setSize(Dimension size); public Insets getInsets(); public void setInsets(Insets insets); public Rectangle getBounds(); public void setBounds(Rectangle bounds);

Cloning and mutable objects


All classes that implement Cloneable should override clone with a public method whose return type is the class itself. This method should call super.clone and then fix any fields that need to be fixed.

Typically this means copying any mutable objects that comprise the internal deep structure of object being cloned and replacing the clones references to these objects with references to the copies.

Reference
1. Absolute JAVA Walter Savitch 2. Effective Java Joshua Bloch Websites
www.sun.com

www.google.com

Potrebbero piacerti anche