Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

How to make a class immutable?

Answer Posted / java

1.Make the class as final
2.Make the data members as private
3.write the public getter methods

Ex:
public class ClassImmutable {


public static void main(String[] args) {
ImmutableTest it=new ImmutableTest(10,"Count");
System.out.println(it.getString());
System.out.println(it.getValue());

}

}

final class ImmutableTest{
private int i;
private String str;
public ImmutableTest(int i,String str){
this.i=i;
this.str=str;

}
public int getValue(){
return i;
}

public String getString(){
return str;
}
}

Is This Answer Correct ?    0 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain the importance of thread scheduler in java?

1016


Explain the transient field modifier?

1048


What is arraylist e in java?

1058


List the features of java programming language.

1050


What is incompatible types in java?

1039


What are classloaders?

1020


What does a void function return?

1006


How to sort an array from smallest to largest java?

986


What is the primary benefit of encapsulation?

1085


What does the exclamation mark mean in java?

1250


How a variable is stored in memory?

990


Can you explain the final method modifier?

1010


Can we use catch statement for checked exceptions when there is no chance of raising exception in our code?

1035


What is the purpose of garbage collection in java?

1097


What is the difference between scrollbar and scrollpane?

1090