12.this, Final, Super, Static Keywords and Singleton Design
12.this, Final, Super, Static Keywords and Singleton Design
}
class B extends A // Compilation error [A is final and non-inheritable]
{
}
public class Test
{
public static void main(String[] args) {
B obj=new B();
}
}
final method-It is used to prevent overriding
class A
{
final void show()
{
System.out.println("A");
}
}
class B extends A
{
void show()//Compilation Error:show() in B cannot override show() in A(Overridden method is final)
{
System.out.println("B");
}
}
public class Test
{
public static void main(String[] args) {
B obj=new B();
obj.show();
}
}
Immutable class
Immutable class means that once an object is created, we cannot change its
content. In Java, all the wrapper classes (like Integer, Boolean, Byte, Short) and
String class is immutable. We can create our own immutable class as well.
Following are the requirements:
The class must be declared as final (So that child classes can’t be created)
Data members in the class must be declared as private (So that direct access is not
allowed)
Data members in the class must be declared as final (So that we can’t change the
value of it after object creation)
Only getters methods for accessing the values/ or parameterized constructors
must be there to assign the value
No setters should be there, so that there is no option to change the value of
instance variables.
Example
final class Employee{
final String pancardNumber;
public Employee(String pancardNumber){
this.pancardNumber=pancardNumber;
}
public String getPancardNumber(){
return pancardNumber;
}
}
public class Main
{
public static void main(String[] args)
{
Employee obj=new Employee("123");
System.out.println(obj.getPancardNumber());
}
}
‘this’ Keyword
• 'this' is used for pointing the current class instance.
• Within an instance method or a constructor, this is a reference
to the current object — the object whose method or constructor
is being called.
class ThisDemo1{
int a = 0;
int b = 0;
ThisDemo1(int x, int y)
{
this.a = x;
this.b = y;
}
}
}
‘static’ Keyword
• used to represent class members
• Variables can be declared with the “static” keyword.
static int y = 0;
• When a variable is declared with the keyword “static”, its called
a “class variable”.
• All instances share the same copy of the variable.
• A class variable can be accessed directly with the class, without
the need to create a instance.
class T2 {
static int triple (int n)
{return 3*n;}
}
class T1 {
public static void main(String[] arg)
{
System.out.println( T2.triple(4) );
T2 x1 = new T2();
System.out.println( x1.triple(5) );
}
}
• Methods declared with “static” keyword are called “class
methods”.
• Otherwise they are “instance methods”.
• Static Methods Cannot Access Non-Static Variables.
• The following gives a compilation error, unless x is also static.
class T2 {
int x = 3;
static int returnIt () { return x;}
}
class T1 {
public static void main(String[] arg) {
System.out.println( T2.returnIt() ); }
}
‘super’ Keyword
• ‘super’ keyword is used to:
– invoke the super-class constructor from the constructor
of a sub-class.
super (arguments if any);
Advantage:
Saves memory because object is not created at each
request. Only single instance is reused again and
again.
• Usage of Singleton Design Pattern