Lecture 4 Static Data Member and Garbage Collection
Lecture 4 Static Data Member and Garbage Collection
✓ Garbage collection
Default field values
radius = 1
Before: After:
Before: After:
c1 c1
i 1 i 2
c2 c2
j 2 j 2
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
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
finalize()
✓ 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