0% found this document useful (0 votes)
11 views

17.java SE (Keyword This and Static)

Uploaded by

vatasem985
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

17.java SE (Keyword This and Static)

Uploaded by

vatasem985
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

INTRODUCTION TO JAVA PROGRAMMING

LANGUAGE

(ELECTIVE-1)
LECTURE-17
Today’s Agenda

• Using “this” keyword.

• Using the keyword “static”.

• static data members

• Garbage collector
The “this” Keyword

• The “this” keyword in java is a predefined object reference


available inside every non static method of a class.
• On calling a method, the java compiler transfers the address
of the object to the called method.
• This address is copied inside the “this” reference. In short
“this” reference points to the object which is currently being
used to call a method.
 Two major benefits of using “this” reference
1. We can use the local variables by using the same name as
that of the data members of class.
2. We can perform inter constructor call using “this”.
Resolving the issue of
local variable and data member
names

• Example :- S.O.P(“Breadth= ”+this.b);


class Box S.O.P(“Height= ”+this.h);
}
{
}
private int l,b,h;
public Box( int l, int b, int h)
{
this.l=l;
this.b=b;
this.h=h;
}
public void show( )
{
S.O.P(“Length= ”+this.l);
Perform Inter constructor
calls

• 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

• The keyword static can be used at three situation i.e.

1. static data members

2. static methods

3. static blocks

4. static classes(Can be used only with nested class or


inner class and not the outer class)
“static” Data members

• Usually, a non static data members is allocated in RAM only


when an object is created.
• static members are saved in RAM once, i.e. they are
independent of the objects.
• A data member is made static when it should display same
number change for all objects.
• For example,
class Data
Both a and b will get space in memory
{ when Object of class Data gets created.
int a;
int b; What if b is made static???
}
Example
class Data Static member
{ a 30
40
0 a
int a; Class member b
10 20
static int b;
} Shared member
class UseData
d1 d2
{
public static void main(String [ ] args) RAM
{
Data d1=new Data( );
Data d2=new Data( );
d1.a=10;
d2.a=20;
System.out.println(d1.a+“\n”+d2.a);
d1.b=30;
d2.b=40;
System.out.println(d1.b+“\n”+d2.b);
}
}
Features of “static”
Data member
• Gets allocated in RAM as soon as program is executed,
irrespective of the object.

• Only a single copy is made.

• Since, they are object independent they should be accessed using


class name.
Data.b=30;
Data.b=40;

• Static members are kept in Permanent Generation area of RAM.

• Local variables cannot be made static i.e. not inside a method.


GarbageCollector

• We will understand this concept through a program…


• WAP to create a class called Employee having the following
data members.
1. An ID for storing unique id allocated to every employee.
2. name of employee.
3. age of employee.

• Also provide following methods –


1. A parameterized constructor to initialize name and age.ID
should also be initialized in this cons
2. A method show() to display ID, name and age.
3. A method showNextId() to display ID of next employee.
Example
class Employee
{
private int ID;
private String name;
private int age;
private static int nextId=1;
public Employee(String name, int age)
{
this.name=name;
this.age=age;
this.ID=nextId++;
}
public void show()
{
System.out.println("Id= "+ID+"\nName= "+name+"\nAge= "+age);
}
public void showNextId()
{
System.out.println("Next employee id will be "+nextId);
}
}
Example

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

• In Java every class by default inherits a class named Object.

• This inheritance is done by Java and cannot be avoided by any


programmer.

• Object class is the Super/Parent class of every class. It is super


daddy class.

• It is present in the package java.lang.

• Object class has 8 methods in it. Hence, every class has at least 8
methods.

• finalize( ) method is one of them and we Override this method.


End Of Lecture 17

You might also like