17.java SE (Keyword This and Static)
17.java SE (Keyword This and Static)
LANGUAGE
(ELECTIVE-1)
LECTURE-17
Today’s Agenda
• Garbage collector
The “this” Keyword
• Example :- }
class Box public Box(Box P)
{ {
private int l,b,h; this(P.l, P.b, P.h);
public Box( int l, int b, int h) }
{ public void show( )
this.l=l; {
this.b=b; S.O.P(“Length= ”+this.l);
this.h=h; S.O.P(“Breadth= ”+this.b);
} S.O.P(“Height= ”+this.h);
public Box(int s) }
{ }
this(s,s,s);
Class Demo
{
public static void main(String [ ] args)
{
Box b1=new Box(10,20,30);
Box b2=new Box(5);
Box b3=new Box(b1);
b1.show( );
b2.show( );
b3.show( );
}
}
Using “static” Keyword
2. static methods
3. static blocks
class UseEmployee
{
public static void main(String [] args)
{
Employee e=new Employee("Amit",25);
Employee f=new Employee("Rakesh",35);
Employee g=new Employee("Sumit",45);
e.show();
f.show();
g.show();
e.showNextId();
f.showNextId();
g.showNextId();
}
}
Garbage Collector
Example continued
class UseEmployee
{
public static void main(String [] args)
{
// same as previous //
{
Employee x=new Employee("Ram",38);
Employee y=new Employee("Ajay",29);
x.show();
y.show();
x.showNextId();
y.showNextId();
System.gc();
System.runFinalization();
}
}
Garbage Collector
class Employee
{
// same as previous //
protected void finalize( )
{
--nextId;
}
}
* In order to call garbage collector on programmer’s
request, we have to call methods gc() and
runFinalization( ).
The “Object” Class
• Object class has 8 methods in it. Hence, every class has at least 8
methods.