import java.io.*;
class Rect
{
int length;
int breadth;
static int count;
Rect()
{
count++;
}
static
{
count=0;
System.out.println("Inside static block");
}
public static void main(String args[])
{
System.out.println("No of objects:"+Rect.count);
Rect r1,r2;
r1=new Rect();
System.out.println("No of objects:"+r1.count);
r2=new Rect();
System.out.println("No of objects:"+Rect.count);
}
}
Static Methods in java…
Example : Rect5
import java.io.*;
class Rect5
{
int length;
int breadth;
static int count;
Rect5()
{
count++;
}
static
{
count=0;
System.out.println("Inside static block");
}
static void displayCount()
{
System.out.println("No of object:"+count);
}
public static void main(String args[])
{
Rect5 r1,r2;
Rect5.displayCount();
r1=new Rect5();
r1.displayCount();
r2=new Rect5();
r2.displayCount();
Rect5.displayCount();
}
}