A final variable behaves like a constant as its value never changes If a final variable is assigned, it always contains the same value.
If final variable holds a reference to an object, then the state of the object may be changed by the operations on object, but the variables will refers to the same object. This applies also to arrays, because arrays are objects; if a final variable holds a reference to an array, then the components of the array may be changed by operations on the array, but the variable will always refer to the same array.
public class ExFinal { private final String name; private final String age; private final Object country ="India"; public ExFinal(String name, int age) { this.name = name; this.age = age; } public String toString() { return name + ", " + age + " " + " from " + " " +country; } }
No comments:
Post a Comment