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

What is a qualifier in a sentence?

925


Is java type safe?

915


Where pragma is used?

992


Why we use protected in java?

932


Can we overload the main() method?

985


Why java is a platform independent? Explain

1005


What is memory leak and how does java handle it?

934


What is the purpose of static methods and static variables?

999


What is object-oriented programming?

1026


What is private public protected in java?

987


What are alternatives to java serialization?

1052


Can we override private constructor in java?

911


What is a byte string?

1051


What is the default value of the local variables?

959


What does it mean that a class or member is final?

939