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?

Answers were Sorted based on User's Feedback



How to make a class immutable?..

Answer / vaibhav

1 Make the all variables of class final and private
2. Make only private constructor

Is This Answer Correct ?    5 Yes 7 No

How to make a class immutable?..

Answer / 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

How to make a class immutable?..

Answer / raju

Just Make the class as a final

Is This Answer Correct ?    29 Yes 41 No

How to make a class immutable?..

Answer / teja

dont provider setters and provide the parameter constructor

Is This Answer Correct ?    10 Yes 27 No

How to make a class immutable?..

Answer / rahul

create a class , having some private variables and setter
method for that variable , In constructor pass the
arguments to set this variable.

Is This Answer Correct ?    7 Yes 33 No

Post New Answer

More Core Java Interview Questions

What are the advantages of user defined functions?

0 Answers  


Give differences between Quicksort &Mergesort. When should these sorts be used andwhat is their running time in java?

0 Answers   Genpact,


Can private method static?

0 Answers  


Is alive method in java?

0 Answers  


how cani read a command line argument?(usingfile object).

3 Answers  


What is the abstraction?

0 Answers  


What are the differences between include directive and include action?

0 Answers  


How java is similar to c?

0 Answers  


What is the difference between serial and throughput garbage collector?

0 Answers  


What is yield () in java?

0 Answers  


Hi i am creating desktop application in that i want calling to mobile number. i have java telephone api (JTAPI) but i dont understand how it configure & use plese help me

0 Answers  


Explain Public static void main?

2 Answers   IBM, Vetech,


Categories