Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


bhaskarreddy
IMMUTABLE
2 Answer(s)      5 years and 3 months ago
Posted in : Java Interview Questions



Advertisement
View Answers

October 13, 2009 at 4:11 PM


Hi Friend,

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 Tutorials/Questions & Answers:
immutable class
immutable class  How to make a class immutable
String immutable - Java Beginners
String immutable  Why is String immutable?  See here Why String is immutable in Java
Advertisements
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
what is mutable and immutable object in java ?difference ?
what is mutable and immutable object in java ?difference ?  what is mutable and immutable object in java ?difference
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
StringBuffer related.
StringBuffer related.  how to make StringBuffer as immutable
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
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
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
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
(ii) String and String Buffer Class
between String and StringBuffer 1) String object is immutable
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
What is ROWID?
of the Oracle ROWID datatype. It is intended to be immutable. The user should not try
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
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
wrapper classes
of the primitive wrapper classes in Java are immutable i.e. once assigned a value
String and stringbuffer object - Java Beginners
are constants and immutable while StringBuffer objects are not. 4)StringBuffer
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
'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
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
Corejava Interview,Corejava questions,Corejava Interview Questions,Corejava
Java Interview Questions       Core java Interview Question page1 An immutable class is a class to which values assigned to the variables can not be altered
java - JDBC
are immutable i.e. once assigned a value to a wrapper class instance cannot be changed
java basics
are immutable i.e. once assigned a value to a wrapper class instance cannot be changed
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
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
java - JDBC
String objects are immutable they can be shared. For example: String str
Wrapper Classes
classes in Java are immutable i.e. once assigned a value to a wrapper class
Advertisements
 

 

 

DMCA.com