Multiple Class Initializer and Initializer Block in JAVA

Multiple Class Initializer and Initializer Block in JAVA

Today My one of the Student ask me questions as per following

1) Can we have Multiple Class Initializer and Initializer Block in One Class?
2) If Yes then What will be the Execution Sequence ?

Following Code will help you find answers.

class A 
{

//Class Initializer Block - 1 
static 
{
System.out.println("1");
}

//Class Initializer Block - 2
static
{
System.out.println("2");
}

//Initializer Block - 1 
{
System.out.println("3");
}


//Initializer Block - 2
{
System.out.println("4");
}

A()
{
System.out.println("5");
}

public static void main(String s[])
{
A a = new A();
}
}

Output :

E:\>java A
1
2
3
4
5

Object Class Functions

Object Class Functions

1) equals
2) toString
3) hashCode

class Cube
{

int h;

Cube()
{
h=3;
}

Cube(int w)
{
this.h=w;
}

}

class Rect extends Cube
{

int l,b;

Rect()
{
l=0;
b=0;
}

Rect(int i)
{
super(i);
this.l=i;
this.b=i;
}

Rect(int i, int j)
{
super(i);
this.l=i;
this.b=j;
}

Rect(int i, int j,int k)
{
super(k);
this.l=i;
this.b=j;
}

//toString() Overloading
public String toString()
{
 return " Length : "+l+" Breath : "+b;
}

//equals() overloading
public boolean equals (Object o)
{
return ((this.l * this.b)==(((Rect)o).l * ((Rect)o).b));
}

public static void main(String s[])
{
Rect r1 = new Rect(6);
Rect r2 = new Rect(12,3,6);
Rect r3 = new Rect(9,4);
Rect r4 = new Rect(6);

System.out.println(r1.equals(r2));
System.out.println(r2.equals(r3));
System.out.println(r3.equals(r1));
System.out.println(r1);
System.out.println(r2+ " " +r3);

//How to Use hashCode() Function
System.out.println(r1.hashCode());
System.out.println(r4.hashCode());
        
}
}

Anonymous Object in JAVA

Anonymous Object in JAVA

class A  {

int k;

A(int m)  {
k=m;
}

int getK() {
  return k;
}

}


class TestDemo {

public static void main(String s[])  {

System.out.println(new A(10).getK());

}

}

Watch Following Video to Learn in Detail :



Static Variable

Demonstrating Use of Static Variable.

class Student
{

int rollno=0;
static int boy=0;
static int girl=0;


        // Value 1-Boy 2-Girl Constructor
Student(int no)
{
if(no==1)
{
boy++;
rollno=boy + girl;
}
else
{
girl++;
rollno=boy + girl;
}

}

static void TotalObj()
{
System.out.println(boy + girl);
}

}

class C
{

public static void main(String s[])
{
Student sanjay=new Student(1);
Student amit=new Student(1);
Student sonali=new Student(2);
Student dipti=new Student(2);
System.out.println("Sanjay Roll No : "+sanjay.rollno);
System.out.println("Dipti Roll No : "+dipti.rollno);
System.out.println("Total Boys: " + sanjay.boy);
System.out.println("Total Girls: " + Student.girl );
System.out.print("Total Students : ");
Student.TotalObj();
}
}

Initializer and Class Initializer Block

class A
{

// Class Initializer Block - Will called First
static
{
System.out.println("Class Initializer Block Called");
}

// Initializer Block - Will called at time of Object Creation
{
System.out.println("Initializer Block Called");
}

public static void main(String s[])
{

System.out.println("Program Stated - Main Block Called");
A a=new A();

}
}

OUTPUT :

Class Initializer Block Called
Program Stated - Main Block Called
Initializer Block Called

Reverse the Words of String using JAVA

Reverse the Words of String using JAVA

class A1
{
public static void main(String args[])
{

//Input String
String text="gardi mca gtu sooadm java";

//Output String
String s1="";

int k2,k1,i=0,j=0;
//Set both pointer to End of String
k2=k1=text.length()-1;

//Loop for moving in String from END to START
for(i=k2;i>=0;i--,k1--)
{

//Checking for SPACE and First Character
 if(text.charAt(i)==' ' || i==0)
{

//Creating New String
for(j=(i==0)?0:i+1;j<=k2;j++)
{
s1=s1+text.charAt(j);
} // For Loop of j

s1=s1+' ';
k2=k1-1;

} // IF condition

} //For Loop of i

System.out.println(s1);
} // main

} //Class

Primitive Data Types Relations

Primitive Data Types Relations in JAVA