Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Lecture 4

Static Data Members and


Garbage Collection
Outcome of the Lecture

Upon completion of this lecture you will be able to

✓ Differentiate primitive and reference datatype

✓ Use static keyword with different data members

✓ Understand garbage collection


Outline of the Presentation

✓ Default field values

✓ Difference between reference and primitive type variable

✓ Static variables, constants and methods

✓ Garbage collection
Default field values

Default Value for a Data Field


reference type → null
numeric type → 0
boolean type → false
char type → '\u0000‘
* No default value to a local variable inside a method.
public class Student {
String name; // name has default value null
int age; // age has default value 0
boolean isScienceMajor; // isScienceMajor has default value false
char gender; // gender has default value '\u0000'
}
Difference between variables of primitive types and reference types
Created using new Circle()
Primitive type int i = 1 i 1

Object type Circle c c reference c: Circle

radius = 1

Copying Variables of Primitive Data Types and Object Types

Primitive type assignment i = j Object type assignment c1 = c2

Before: After:
Before: After:
c1 c1
i 1 i 2
c2 c2
j 2 j 2

c1: Circle C2: Circle c1: Circle C2: Circle


radius = 5 radius = 9 radius = 5 radius = 9
Static Variables, Constants and Methods

✓ Stores values for the variables in a common


memory location
✓ Static variables are shared by all the instances
Data Member
of the class.
✓ If one object changes the value of a static
instance static variable, all objects of the same class are
(variables, constants, (variables, constants,
methods and nested class) methods and nested class) affected
✓ Static constants are final variables shared by all
the instances of the class.
✓ Static method can be called without creating
any instance of the class
✓ To declare static variables, constants, and
methods, use the static modifier.
Static Variables, Constants and Methods

Static Variables, Constant and Methods


Class C

static variable;
Instance variable;

Static method
• Can access static variable directly;
• Can access Instance variable through
object of class C;

Instance method
• Can access static variable directly;
• Can access Instance variable directly;
Static Variables, Constants and Methods
Accessing within same class
public class Tester Class C
{
int i = 10;
static int j = 20; static variable;
void methodOne() Instance variable;
{
System.out.println("i = "+i); Static method
System.out.println("j = "+j);
} • Can access static variable directly;
static void methodTwo() • Can access Instance variable through
{ object of class C;
Tester t =new Tester();
System.out.println("i = "+t.i);
System.out.println("j = "+j); Instance method
}
public static void main(String[] a) • Can access static variable directly;
{ • Can access Instance variable directly;
Tester t =new Tester();
t.methodOne();
methodTwo();
}
}
Static Variables, Constants and Methods
Accessing outside the class

class Tester
public class Test
{
{
int i = 10;
public static void main(String[] a)
static int j = 20;
{
void methodOne()
Tester t =new Tester();
{
t.methodOne();
System.out.println("i = "+i);
Tester.methodTwo();
System.out.println("j = "+j);
}
}
}
static void methodTwo()
{
Tester t =new Tester();
System.out.println("i = "+t.i);
System.out.println("j = "+j);
}
}
Static Variables, Constants and Methods
//Java Program to demonstrate the use of an
instance variable public static void main(String args[]){
//which get memory each time when we //Creating objects
create an object of the class. Counter c1=new Counter();
class Counter{
Counter c2=new Counter();
int count=0;//will get memory each time
when the instance is created Counter c3=new Counter();
Counter(){ }
count++;//incrementing value }
System.out.println(count); Output
} 1
1
1
Static Variables, Constants and Methods
/*Java Program to illustrate the use of public static void main(String args[]){
static variable which is shared with all
objects. */ //creating objects
class Counter2{ Counter2 c1=new Counter2();
static int count=0;//will get memory only Counter2 c2=new Counter2();
once and retain its value
Counter2 c3=new Counter2();
Counter2(){ }
count++;//incrementing the value of static }
variable Output
System.out.println(count);
1
}
2
3
Static Variables, Constants and Methods
Shared static variable class Circle
{
static int count=0;
public Circle()
{ count++;
}
int getCount()
{
return(count);
}
}
public class exStatic
{
public static void main(String[] a)
{
Circle c1 = new Circle();
System.out.println(Circle.count);
Circle c2 = new Circle();
System.out.println(Circle.count);
Circle c3 = new Circle();
System.out.println(Circle.count);
}
}
Static Variables, Constants and Methods

//Java Program to get the cube of a given number using the static method

class Calculate{
static int cube(int x){
return x*x*x;
}
}
class testcal
{
public static void main(String args[]){
int result=Calculate.cube(5);
System.out.println(result);
}
}
Static Variables, Constants and Methods
class A{ class A2{
int a=40;//non static
static
{
public static void main(String args[]){
System.out.println(a);
System.out.println("static block is
invoked");
}
}
}
public static void main(String args[]){
Output System.out.println("Hello main");
? }
}
Static Variables, Constants and Methods
//Java Program to demonstrate the use of a static method. public class TestStaticMethod{
class Student{ public static void main(String args[]){
int rollno;
String name;
Student.change();//calling change method
static String university = “MU"; //creating objects
//static method to change the value of static variable Student s1 = new Student(111,"Karan");
static void change(){
university = “Marwadi"; Student s2 = new Student(222,"Aryan");
} Student s3 = new Student(333,"Sonu");
//constructor to initialize the variable
//calling display method
Student(int r, String n){
rollno = r; s1.display();
name = n; s2.display();
}
s3.display();
//method to display values
void display(){System.out.println(rollno+" "+name+" }
"+university);}
}
}
Output
Garbage Collection

Garbage

✓ Objects with no references


✓ Garbage occupies memory space

When no references to an object exist, that object is assumed to be no longer needed, and
the memory occupied by the object can be reclaimed

Garbage Collection

✓ Garbage is automatically collected by JVM


✓ The Java runtime system detects garbage and automatically reclaims the space it occupies
Garbage Collection

Advantages of garbage collection


Java can make programmers more productive.
Programming in non-garbage-collected languages, the programmer has to spend a lot of
time in keeping track of the memory de-allocation problem. In Java, the programmer can
use that time more advantageously in doing other work.
It ensure program integrity.
Garbage collection is an important part of Java’s security strategy. Java programmers feel
free from the fear of accidental crash of the system because JVM is there to take care of
memory allocation and de–allocation.
Garbage Collection

Disadvantages of garbage collection


The major disadvantage of a garbage-collected heap is that it
adds an overhead that can adversely affect program
performance.
The JVM has to keep track of objects that are being referenced by the executing program, and free unreferenced objects on the fly. This
activity will likely take more CPU time than would have been required if the program explicitly freed unrefined memory.

The second disadvantage is the programmers in a garbage-


collected environment have less control over the scheduling
of CPU time devoted to freeing objects that are no longer
needed.
Garbage Collection

finalize()

✓ Contains action to be performed when object is destroyed

✓ e.g. releasing resources held by object to be destroyed

✓ To add a finalizer to a class, define the finalize( ) method which is called by the Java run
time whenever it is about to recycle an object of that class

✓ protected void finalize( ){


// finalization code here
}
✓ finalize( ) is only called just prior to garbage collection, not when an object goes out-of-
scope

You might also like