Wednesday, 27 April 2016

Know about Static and Instance Blocks in Java

Static Block in Java:-
The purpose of static block is to initialize static data member’s only once static block and execute only once when the class is loaded into the main memory before executing the main method.
Syntax:-
Static
{
Block of statements (s); ……….initialize of static data members.
}
When we write a static block and main method in a single Java program then JVM executes static block first and later main method only once.
Writing block is optional when we write static block & constructors in a Java program then JVM will executes static block only once & constructors will execute each and every time.
In static block we can access / initialize only static data members but not instance data members where as both instance & static data members can be access in constructor.
In one Java class we can write any number of static blocks but all these types of static blocks will be treated as single static block and gives the output in that order in which we write.
Whenever we initialize a static data member in the class then by default that class will contain then system defined static block.
** write a Java program which will illustrate the concept of static block.
class SBEX1
{
static
{
System.out.println("I am From Static Block");
}


public static void main(String[] args)
{
System.out.println("I am from main()");
}
}
Instance Block:-
The purpose of instance block is exactly similar to default constructor of the particular class.
Instance block will execute each and every time whenever an object is created before executing the constructor and after executing the static block.
Static block will execute only once when the class is loaded in the main memory for initializing the data in main memory whereas instance block will be calling each and every time whenever an object is created before the constructor and it is used for initializing instance data members of a class.
Syntax:-
{
Block of statements (s); ……….initialize of instance data members.
}
We can write any number of instance blocks but all of the instance block will be treated as single instance block and they will be executed in which order they write.


** write a Java program which will illustrate the concept of instance block.
class IBEX1
{
static
{
System.out.println("SB...........IBEX1");
}
{
System.out.println("IB............IBEX1");
}
IBEX1()
{
System.out.println("IBEX1..........DC");
}
}


public static void main(String[] args)
{
System.out.println("I am from main()");
IBEX1 t1=new IBEX1();
IBEX1 t2=new IBEX1();


}

}

Monday, 4 April 2016

Parameterized Constructor in Java

Know About Parameterized Constructor in Java........

Parameterized Constructor: - a constructor is said to be parameterized if and only if constructor always takes parameters.
The purpose of parameterized constructor is that it always takes parameters and creates multiple objects with respect to same class for placing different values.
Syntax:-                 class <clsname>
                                 {
                                       ……………
                                      …………….
                                  <clsname>(list of formal parameters)
                                      {
                                        Block of statements(s)……….initialize
                                    }
                                   …………………
                                   …………………
}

Write a java program which illustrates the concept of parameterized constructor.
class Constructor2
{
            int a,b;
            Constructor2(int x,int y)
            {
                        System.out.println("Constructor..........DPC");
                        a=x;
                        b=y;
                        System.out.println("The value of a is="+a);
                        System.out.println("The value of b is="+b);
            }
}



class Constructor2Demo
{
            public static void main(String k[])
            {
                        Constructor2 c1=new Constructor2(10,20);
                        Constructor2 c2=new Constructor2(100,200);
                        Constructor2 c3=new Constructor2(1000,2000);
                       
            }
}

Rule2:- when we create an object with parameterized constructor then defining the parameterized constructor is mandatory otherwise we get compile time error.
Rule3:- whenever we create multiple objects with both default & parameterized constructor then it is mandatory to the java programmer to define both default & parameterized constructors otherwise we get compile time error.
Note: - if we are not initializing data members of any class with respect to corresponding class constructors then the uninitialized data members will be initialized by system defined default constructor with default values.
Overloaded Constructor: - a constructor is said to be overloaded if and only if constructor name is same but signature is different.
Signature represents the following point…….
No. of parameters
Type of parameters
Order of parameter
At least one thing must be different
Test t1=new Test(10,20);
Test t2=new Test(100);
Test t3=new Test(1.5f,2.5f);
Test t4=new Test(10,1.5f);
Test t5=new Test(1.5f,20);
Here Test(. . .) is known of overloaded constructor.
Object parameterized constructor:- a constructor is said to be object parameterized if and only if it always takes object as a parameter.
The purpose of object parameterized constructor is that to copy the content of one object into another object belongs to same type.
Note: - the functionality of object parameterized constructor resembles the copy constructor of c++.