/*
* Person is an immutable class. Person's
* state cannot be changed once it is created.
*
* We can't subclass the final class and
* alter the behavior.
*/
public final class Person
{
final String name;
public Person(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
ImmutableTest.java public class ImmutableTest
{
public static void main(String[] args)
{
Person personObj = new Person("Peter");
System.out.println(personObj.name);
System.out.println(personObj.getName());
//personObj.name = "Dave";
}
}