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

Why is java called java?

1102


How do you implement tree mirroring in java?

1125


What is javac_g?

1070


what are the states associated in the thread? : Java thread

1111


What is array in java?

1057


What is nan in java?

1056


What is the purpose of the file class in java programming?

1097


What is length in java?

1062


What is advantage of java?

1070


What is the purpose of using break in each case of switch statement?

1068


Is finalize() similar to a destructor?

1078


How can we create a synchronized collection from given collection?

1130


Explain about procedural programming language or structured programming language and its features?

1173


What environment variables do I need to set on my machine in order to be able to run java programs?

1063


What do you mean by an object in java?

1186