By
Dr. Yasser Abdelhamid
final keyword
this keyword
static methods (revisited)
static block
Instance Initialization Block(IIB)
Static Initialization Block(SIB)
Thejava final keyword is used to restrict
the use of:
Variables,
Methods, and
Classes
Value of variables defined as final, cannot be
changed(like a constant).
If the variable is a parameter, its value cannot be
changed also.
class MyClass{
final int weight=90;//final variable
void changeWeight(final int height){
weight=80; // Error, can’t change final variable
height= 180; // Error height is final
}
public static void main(String args[]){
MyClass obj=new MyClass();
obj.changeWeight(89);
}
}
Final method cannot be overridden, but
still can be inherited.
final is not allowed for Constructors.
class Bike{
final void run(){System.out.println("running");}
}
class Honda extends Bike{
void run(){System.out.println("running safely with
100kmph");} // cannot override final method
public static void main(String args[]){
Honda honda= new Honda();
honda.run();
}
}
Final class cannot be inherited.
final class Bike{}
class Honda1 extends Bike{
void run(){System.out.println("running
safely with 100kmph");}
public static void main(String args[]){
Honda1 honda= new Honda1();
honda.run();
}
}
thisis a reference variable that refers to
the current object.
There are many ways to use this keyword:
referencing current class instance variable.
calling current class method (implicitly)
calling current class constructor.
can be passed as an argument in the method call.
can be passed as argument in the constructor call.
can be used to return the current class instance from
the method.
In
case of ambiguity between the instance
variables and parameters, this keyword
resolves this problem.
public class Person {
private String name;
private intage;
public void setName(String name) {
this.name= name;
}
public String getName() {
return name;
}
}
class A{
void m(){System.out.println("hello m");}
void n(){
System.out.println("hello n");
//m();//same as this.m()
this.m();
}
}
class TestThis4{
public static void main(String args[]){
A a=new A();
a.n();
}
}
this() method refers to current class
constructor.
It is used to reuse the constructor.
it must be the first line in the constructor.
class A{
A(){
this('a');
System.out.println("Default constructor");}
A(char c){ System.out.println("char");}
A(int x){
this(); // call default constructor
System.out.println(x);
}
}
class TestThis5{
public static void main(String args[]){
A a=new A(10);
}
}
Passing
current object as an argument to
a method.
class Student{
void m(Student obj){
System.out.println("method is invoked");
}
void p(){
m(this);
}
public static void main(String args[]){
Student s1 = new Student();
s1.p();
}
}
class A{
A getA(){
return this;
}
void msg(){System.out.println("Hello java");}
}
class Test1{
public static void main(String args[]){
new A().getA().msg();
}
}
A static method can access static data
member and can change its value.
The static method can not use non static
data member or call non-static method
directly.
this and super cannot be used in static
context.
Is used to initialize the static data
member.
It is executed before the main method at
the time of class loading.
class A2{
static{
System.out.println(“Loading class…");
}
public static void main(String args[]){
System.out.println(“Start main method");
}
}
IIB run each time when an instance of the
class is created.
It is used to initialize the instance data
members.
The instance initializer block is invoked
after the parent class constructor is
invoked (i.e. after super() constructor
call).
class A{
A(){ System.out.println("parent class constructor invoked"); }
}
class B2 extends A{
B2(){
super();
System.out.println("child class constructor invoked");
}
{System.out.println("instance initializer block is invoked");}
public static void main(String args[]){
B2 b=new B2();
}
}
// (see IIBSIB.java in OOP project)
Output: parent class constructor invoked
instance initializer block is invoked
child class constructor invoked
Static Initialization Block(SIB) can be used
to initialize static variables or to call a
static method.
Static block code executes only once
during the class loading.
Instance Initialization Block (IIB) is
executed every time an instance of the
class is created, and it can be used to
initialize the instance data members.
A class can have multiple static blocks,
and they will execute in the same order as
they appear in the class.
Static Initialization Block Instance Initializer Block
It executes during class
It executes during class loading
instantiation
It can use static or non-static
It can only use static variables
(instance variables).
It can not use this It can use this
It executes only once during the
entire execution of the program It can run many times whenever
when the class loads into the there is a call to the constructor
memory
In payroll systems, it is often needed to
print a salary check for each employee in
numbers and words.
For example 16654 is written as
Sixteen thousand, six hundred, and fifty
four.
Write the code that implements this
function in Java.
You may need it soon
What parts of the project, can we do now?