2.4 Constructor.

Constructor :
new constructor();

new -> allocate memory , load non-static member (copy of class), call constructor of class.
constructor() -> initialize data member.

Every class must have constructor.
1. defined by compiler : default constructor.
2. defined by programmer : user defined constructor.

Sysntax :

constructorName(arguments) //declaration.
{
// code to initialize data member.
}

-> Constructor name must be same as class name.
-> Constructor should not specify return type.

Notes :

1. In a static context we can refer only static member of that class directly.
2. In a non-static context both static and non-static member of the current class can be access directly.
3. In the static context we need to create the instance of the class to the access non-static members.

Example Program1 :

class Demo1
{
int k;
int j;
Demo1() //user defined constructor.
{
System.out.println("running Demo1 constructor");
k = 62;
j = 45;
}
}
class MainClass1
{
public static void main(String args[])
{
System.out.println("program started");
Demo1 d1 = new Demo1(); //instance created using user defined constructor of Demo1 class.
System.out.println("k value is"+d1.k);
System.out.println("j value is"+d1.j);

}
}

Example Program2 :

class Demo2
{
int k;
int j;
Demo2(int arg1, int arg2) //user defined parameterized constructor.
{
System.out.println("running Demo2 constructor");
k = arg1;
j = arg2;
}
}
class MainClass2
{
public static void main(String args[])
{
System.out.println("program started");
Demo2 d1 = new Demo2(100, 45); //instance created using user defined constructor of Demo2 class.
System.out.println("k value is"+d1.k);
System.out.println("j value is"+d1.j);

}
}


Notes :

1.  Constructor are special members of the class which is used to initialize the data members of the class.
2. Every class must have a constructor either defined by compiler or defined by user.
3. The constructor defined by compiler is known as default constructor.
4. The default constructor will be created only when the class is not having any user defined constructor.
5. The default constructor will not have any arguments.
6. if the constructor are defined by user than its known as user defined constructor.
7. user can create '0' parameter constructor as well as parameterized constructor.
8. Any constructor with argument is known as parameterized constructor.

-> While defining a constructor the constructor name must be same as class name and constructor should not have any return type and not even void.
-> Constructor should not be declare as static.
-> Constructor are used for object initialization hence should not perform any business operation in the constructor body.

Notes :

While designing an object if the object can not be created for that data member than we should go for user defined constructor.
Example :
We can create a notebooks wothout defining the number of page hence the notebooks class should have user defined cunstructor which initializes the page data number.
  
Example Program3 :

class Notebooks
{
int pages;
double price; 
Notebook()//constructor overloading.
{
System.out.printlm("running no-arg constructor");
pages = 100;
price = 35.34;
}
NoteBook(int arg)
{
System.out.printlm("running int-arg constructor");
pages = arg;
price =  45.67;
}
void details()
{
System.out.printlm("No of pages"+pages);
System.out.printlm("Book Price is"+price);
}
}
class MainClass3
{
public static void main(String args[])
{
System.out.printlm("Program Strated");
NoteBooks b1 = new NoteBooks();
b1.details();
NoteBooks b2 = new NoteBooks();
b2.details();
System.out.printlm("Program Ended");
}
}

Notes :

In a class defining multipule constructor with different parameter is known as constructor overloading.
the parameter should differ either in terms of parameter tyoe or parameter length.

Constructor :

-> used to initialized  data member.
-> called by new operator.
-> default or user defined constructor(no-argument constructor or parameterized constructor).
-> constructor overloading.
-> final cannot re-assign.

Final Data members :

1. Initialize @ declaration time.
2. use respective block.
3. use constructor.

Example Program :

1. 
class Demo1
{
final int k = 320;
final static double j =25.64;

}
class MainClass1
{
public static vaoid main(String args[])
{
System.out.println("Program started");
System.out.println("J value is"+Demo1.j);
Demo1. d1 = ne Demo1();
System.out.println("k value is"d1.k);
System.out.println("Program Ended");
}
}
2. //Initialization final data members.

class Demo1
{
final int k;
final static double j;
static
{
j = 45.75;
}
Demo1()
{
System.out.println("running constructor");
}
}

Notes :
-> If a final data member of a class should be initialize during declaration itself.
-> If the final data member static than it must be initialize either at the time of declaration or by using blocks.
-> if the final data member is non-static than we should initialize at the time of declaration or using non-static blocks or using constructor.
-> if every object needs a constant data specified to the object than we gor for non-static data member and it should be initialize through parameterized constructor.

-> this - corrent object address.

Example Program :
class Sample1
{
int k;
double j;
void display()
{
System.out.println("k value"+this.k);
System.out.println("j value"+this.j);
}
}
class MainClass2
{
public static void main(String args[])
{
System.out.println("Program Started");
Sample s1 = new Sample();
Sample s2 = new Sample();
s1.k = 34;
s1.j = 56.77;

s2.k = 892;
s2.j = 12.56;
System.out.println("S1 value :"+s1);
s1.display();
System.out.println("S2 value :"+s2);
s2.display();
System.out.println("Program Ended");
}

Notes :
This Keyword
-> Java Provide keyword by name 'this' which is used to refer current object member.
-> 'this' keyword always points to the cureent object location.
-> when the data member name and the local variable name are same the data member are differentiated prefixing by 'this' keyword.
-> this keyword is only used inside the non-static context , method, block and constructor.
-> if a class is having static block , non-static block and constructor than
static block get executed at the time of class loading than non-static block will be executed than the constructor.










Comments

Popular Posts