How to make a class immutable?

Answer Posted / khizar khan

1) Make a class fianl like final class A{}

2) Then use the property as private and final
ex:
final class A{
private final int salary;

}
3)Now make a Constructor to initilize the values
ex:
A(int a)
{ this .salary=a;
}
4)Now use the gtter mathod to acces the values
ex:
final class A{
private final int salary ;
A( int a)
{
this.salary=a;

}
public int getSalary()
{
return salary;

}

}

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What do you understand by the term wrapper classes?

534


Difference between final and effectively final ? Why is effectively final even required ?

544


Describe method overriding

575


How to sort elements in a parallel array in java?

495


What is Java Annotations?

549






How does linkedhashmap work in java?

505


Why do we need variables?

523


How objects are stored in java?

538


If a class is declared without any access modifiers, where can the class be accessed?

585


Can Exception handling we can handle multiple catch blocks?

637


What are constants?

590


Differentiate between overriding and overloading cases?

586


What is the difference between choice and list?

583


What is double checked locking in singleton?

600


What are the skills required for core java?

530