1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
// class is declared final
final class Immutable {
// private class members
private String name;
private int date;
Immutable(String name, int date) {
// class members are initialized using constructor
this.name = name;
this.date = date;
}
// getter method returns the copy of class members
public String getName() {
return name;
}
public int getDate() {
return date;
}
}
class Main {
public static void main(String[] args) {
// create object of Immutable
Immutable obj = new Immutable("Programiz", 2011);
System.out.println("Name: " + obj.getName());
System.out.println("Date: " + obj.getDate());
}
}
|