To make a class immutable we must restrict changing the state of the class object.We can achieve this through a final and private modifier.Therefore,make all fields final and private.Don't provide any methods that modify the object.
for example,
final class ImmutableClass{ private final int count; public ImmutableClass(int cout){ count = cout; } public int getCount(){ return count; } } public class Immutable{ public static void main(String[] args) { ImmutableClass cl = new ImmutableClass(5); System.out.println(cl.getCount()); } }
Thanks
October 24, 2010 at 10:35 AM
Please see here **Deleted**
Related Pages:
immutable class immutable class How to make a class immutable
IMMUTABLE - Java Interview Questions
IMMUTABLE How To Make A Class Immutable? Hi Friend,
To make a class immutable we must restrict changing the state of the class...;
}
}
public class Immutable{
public static void main(String[] args
Mutable and immutable Objects - Java Beginners
Mutable and immutable Objects Hi,
Please tell me about Mutable and immutable Objects.
What is mutable object and immutable object and how... we can alter. like any primitive. Immutable are those object whcih we can
STRING IMMUTABLE - Java Interview Questions
STRING IMMUTABLE Why string objects are immutable? what is the region behind?i want regione plz replyyyyyyyyy Hi Friend,
JVM maintains... object is immutable.
Thanks
Immutable Image using Canvas Class Immutable Image using Canvas Class
This is the immutable image example which shows how to create the immutable
image using canvas. In this example ImageCanvas class
Immutable Image MIDlet Example Immutable Image MIDlet Example
This is the immutable image example, which shows how to create a immutable
image in MIDlet. In this example we are creating
core java - Java Beginners
core java hi,
we know that String object is immutable in nature so like string object can we create our own object and that object should be immutable i mean we cant modify our own object plzzzzzzzzzzzzz tell me the answer
java interview
java interview what is infterface? and it is uses?
what is ast class? and it is uses?
what is ast methods?
what is immutable?
what is mutable?
what are the final class in java?
what is the static methods
j2me image application
j2me image application i can not get the image in my MIDlet .........please tell me the detailed process for creating immutable image without Canvas class
CORE JAVA
CORE JAVA Static methods can be participated in inheritance?
What is static keyword?
What is static keyword?
How to write code for static factory method?
What is mutable and immutable
What is ROWID?
of the Oracle ROWID datatype. It is intended to be immutable. The user should not try
Java object
Java object What is mutable object and immutable object? An Immutable object is a kind of object whose state cannot be modified after... after it is created.
The basic way to create an immutable object is to make all
Wrapper Class
of the primitive wrapper classes in Java are immutable i.e. once assigned a value to a wrapper... are immutable i.e. once assigned a value to a wrapper class instance cannot be changed
Session Factory
Session Factory Define the session factory interface in hibernate?
Hi Samar,
It creates new hibernate sessions by referencing immutable... sessions by referencing immutable and thread safe objects. Application using
'String' vs 'StringBuffer' - Java Beginners
'String' vs 'StringBuffer' What should i use String or StringBuffer? Hi, First you need to rectify the question. Because StringBuffers append the data, and Strings concatenate the data. Since Strings are immutable
java code - Java Beginners
java code write a program for immutable class Hi Friend,
Try the following code:
final class ImmutableClass{
private final String product;
private final double price;
public ImmutableClass(String
wrapper classes
of the primitive wrapper classes in Java are immutable i.e. once assigned a value
STRINGS - Java Interview Questions
STRINGS 1.Why string objects are immutable?
2.why java is pass by value? Hi Friend,
1) To save memory, java can refer two string... program.Therefore String objects are made immutable.
This provides better memory
java - Java Beginners
java it is define that in wapper class implements immutable object that means that after the primitive value is initilized in the wapper object then there is no way to change that value.but i could not understand what it mean
java basics
are immutable i.e. once assigned a value to a wrapper class instance cannot be changed
java - JDBC
are immutable i.e. once assigned a value to a wrapper class instance cannot be changed
Wrapper Classes
classes
in Java are immutable i.e. once assigned a value to a wrapper class
java - JDBC
String objects are immutable they can be shared. For example:
String str
Few of the familiar Design Patterns in Java
particular class.
5. Immutable
The Immutable pattern reduces the
overhead... state information to change after it is constructed. The
Immutable pattern also... to a shared object. The Immutable object pattern can be used to avoid
the need
Wrapper
in Java are immutable. Converting primitive types into the
corresponding wrapper
NSString in objective c
for an objects(that manages immutable strings). The objects later ..can be used...: Immutable string are those strings that can be defined at the time of creating
j2me - MobileApplications
(this);
exit = new Command("Exit", Command.EXIT, 1);
form = new Form("Immutable Image
j2me - MobileApplications
(this);
exit = new Command("Exit", Command.EXIT, 1);
form = new Form("Immutable Image
Java final keyword
be modified. It must be made immutable so that modifcations are not made.
The final
Summary - Data
have corresponding "wrapper" class whose objects are immutable... methods for working with
characters.
String immutable 0 or more characters
How to make a file in java
Description:
This example demonstrate how to make a file and write string
in it. To create a file in java we use the File Class. The instance of the File
class is immutable that means the pathname represented by the File object
Wrapper Classes
are immutable i.e. once assigned a value
to a wrapper class instance cannot be changed
Hibernate Architecture
with the database. SessionFactory object is a
thread safe (immutable) and it is created once
J2ME Tutorial
and implements the CommandListner class.
Immutable Image MIDlet Example
This is the immutable image example, which shows how to create a immutable
image in MIDlet. In this example we are creating a image