-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUseOfFinalClass.java
More file actions
27 lines (18 loc) · 1016 Bytes
/
UseOfFinalClass.java
File metadata and controls
27 lines (18 loc) · 1016 Bytes
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
package com.interviews;
public class UseOfFinalClass {
public static void main(String[] args) {
FinalClassExample finalClassExample = new FinalClassExample(1, "John", "London");
finalClassExample = new FinalClassExample(1, "John", "London");
System.out.println(finalClassExample.getAddress());
//1. Final means that you can't change the object reference to point to another reference or another object, but you can still mutate its state (by using the setter method). Where immutable means that the object's actual value can't be changed, but you can change its reference to another one.
final StringBuffer sb = new StringBuffer("Hello");
// Even though reference variable sb is final
// We can perform any changes
sb.append("GFG");
System.out.println(sb);
// Here we will get Compile time error
// Because reassignment is not possible for final variable
//sb = new StringBuffer("Hello World");
System.out.println(sb);
}
}