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++.



Wednesday, 30 March 2016

Constructors in java

Know About Constructors in java...........


Constructors in java:- a constructor is one of the special member method and it is automatically / implicitly called by JVM  during object creation time for placing our own values without placing default values.
Advantages of Constructors:-
When we develop the java application with the concept of constructors then such applications will get the following advantages-
·        Constructors eliminate default values.
·        Constructors eliminate in calling ordinary method.
In other word the purpose of constructor concept is to initialize the object.
Rules/Characteristic/properties:-
·        Constructor will be called implicitly/automatically during object creation time.
·        Each and every constructor name is similar to class name.
·        Constructor will not return any value even void also(if we write any return type then it will be treated as an ordinary method )
·        Constructor should not be static.(because whenever an object is created constructors will be called each & every time)
·        Constructors in java programming will not participate in inheritance process (because every constructor is mint for initializing its own class data members )
·        The access modifier of the constructor may or may not be private.
(a)  If the access modifier constructor is private an object of the corresponding class can be created in same class context of some other class.
(b) If the access modifier of the constructor is not private then we can create an object of corresponding class in the same class context and in another class context.

Types of constructors in java:-
 In java programming we have two types of constructors they are ….
·        Default/parameter less/zero argument constructor.
·        Para metered constructor.
1.     Default Constructor:- a constructor is said to be default if at only if it will not take any parameters.
The purpose of default constructor is to create multiple object with respect to same class for placing same values.
Syntax:-                       class <clsname>
                                      {
                                              <clsname>()
                                               {
                                                        Block of statements(s)………….initialization
                                                  }
                                             }
Ø Write a java program which illustrates the concept of default constructor.
  class Constructor
{
      int a,b;
      Constructor()
      {
                  System.out.println("Constructor...........DC");
                  a=1;
                  b=2;
                  System.out.println("the value of a="+a);
                  System.out.println("the value of b="+b);
      }
                 
}



class  ConstructorDemo
{
      public static void main(String k[])
      {
                  Constructor c1=new Constructor();
                  Constructor c2=new Constructor();
                  Constructor c3=new Constructor();

                 
      }
}


Note:- Whenever we create an object only with default constructor then defining the default construction is optional whenever we create an object with default constructor and if we are not defining default constructor then JVM will call its own constructor known as system defined default constructor & whose role is to place default values.

Rule1:- if we create an object with the default constructor and if we define the default constructor then JVM will execute programmer defined default constructor & places programmer defined values.