Pages

Constructor & 'THIS' Keyword

Constructor

1.same name as a class name.
2.it should not have a return type.
3.it cant be static,final or abstract.
4.executes automatically ,when object created.

1)
class C
{
 int i;
 int j=10;
 C()
 {
  i=20;
 }
 public static void main(String[] args)
 {
  C c1=new C();
  System.out.println(c1.i);
  System.out.println(c1.j);
 }
}
OP:
/*
20
10
*/
---------------------------------------------------------------------------
2)
//4 members are there ,constructors also a member
class D
{
 int i,j;
 D()
 {
  i=20;
  j=40;
 }
 public static void main(String[] args)
 {
  D d1=new D();
  System.out.println(d1.i);
  System.out.println(d1.j);
 }
}
OP:
/*
20
40
*/
---------------------------------------------------------------------------
3)
class E
{
 E()
 {
  System.out.println("E()");
 }
 public static void main(String[] args)
 {
  E e1=new E();
  System.out.println("----");
  E e2=new E();
 }
}
OP:
/*
two times constructor will execute,it executes whenever object created.
E()
----
E()
*/
---------------------------------------------------------------------------
4)
//DEFAULT CONSTRUCTOR only will invoke when we create non-parametrized constructor
class G
{
 G()
 {
         System.out.println("G()");
 }
 G(int i)
 {
  System.out.println("G(int)");
 }

 public static void main(String[] args)
 {
  G g1=new G();
  System.out.println("----");
  G e2=new G(20);
  System.out.println("----");
  G g3=new G();
  System.out.println("----");
 }
}
OP:
/*
G()
----
G(int)
----
G()
----
*/
---------------------------------------------------------------------------
5)
class H
{
 H(int i)
 {
         System.out.println("H(int)");
 }
 public static void main(String[] args)
 {
  H h1=new H(10);
  System.out.println("----");
  H h2=new H(20);
  System.out.println("----");
  //H h3=new H();
  System.out.println("----");
 }
}
OP:
/*
H(int)
----
H(int)
----
----
*/
---------------------------------------------------------------------------
6)
class J
{
 J(int x)
 {
         System.out.println("X(int x)");
 }
 J(byte y)
 {
         System.out.println("X(int y)");
 }
 public static void main(String[] args)
 {
  J j1=new J(90);
  System.out.println("Done");
 }
}

X(int x)
OP:
Done
---------------------------------------------------------------------------
7)
class K
{
 K(int i,int j)
 {
         System.out.println("int,int");
 }
 K(double i,int j)
 {
         System.out.println("double,int");
 }
 public static void main(String[] args)
 {
  K k1=new K(10,20);
  System.out.println("Done");
  K k2=new K(90.9,30);
  System.out.println("Done");
 }
}

---------------------------------------------------------------------------
8)
class L
{
 L()
 {
         System.out.println("L()");
 }
 L(int i)
 {
         System.out.println("L(int)");
 }
 L(int i,int j)
 {
         System.out.println("L(int,int)");
 }
 public static void main(String[] args)
 {
  L l1=new L();
  System.out.println("----");
  L l2=new L(10);
  System.out.println("----");
  L l3=new L(10,20);
  System.out.println("----");
 }
}

'this()' keyword
9)
class M
{
 M()
 {
         System.out.println("M()");
 }
 M(int i)
 {
  this();
         System.out.println("M(int)");
 }
 public static void main(String[] args)
 {
  M m1=new M();
  System.out.println("----");
  M m2=new M(10);
  System.out.println("----");
 }
}
OP:
/*
we can call another constructor from one constructor by using 'this()',this() remains in the same class.(***)
M()
----
M()
M(int)
----
*/

---------------------------------------------------------------------------
10)
class N
{
 N()
 {
         this(10);
     System.out.println("N()");
 }
 N(int i)
 {
         System.out.println("N(int)");
 }
 public static void main(String[] args)
 {
  N n1=new N();
  System.out.println("----");
  N n2=new N(10);
  System.out.println("----");
 }
}
OP:
/*
N(int)
N()
----
N(int)
----
*/
---------------------------------------------------------------------------
11)
class O
{
 O()
 {
           this(2,5);
    System.out.println("O()");
 }
 O(int i)
 {
  this();
         System.out.println("O(int)");
 }
 O(int i,int j)
 {
  System.out.println("O(int,int)");
 }
 public static void main(String[] args)
 {
  O o1=new O();
  System.out.println("----");
  O o2=new O(10);
  System.out.println("----");
  O o3=new O(10,30);
  System.out.println("----");
 }
}
OP:
/*
O(int,int)
O()
----
O(int,int)
O()
O(int)
----
O(int,int)
----
*/
---------------------------------------------------------------------------
12)
class P
{
 P()
 {
    System.out.println("P()");
 }
 P(int i)
 {
  this();
         System.out.println("P(int)");
 }
 P(char c1)
 {
  this();
         System.out.println("P(char)");
 }
 P(boolean b1)
 {
  this('a');
  System.out.println("P(Boolean)");
 }
 P(double d)
 {
  this(10);
  System.out.println("P(double)");
 }
 public static void main(String[] args)
 {
  P o1=new P();
  System.out.println("----");
  P o2=new P('a');
  System.out.println("----");
  P o3=new P(10);
  System.out.println("----");
  P o4=new P(10.9);
  System.out.println("----");
  P o5=new P(false);
  System.out.println("----");
 }
}
OP:
/*
P()
----
P()
P(char)
----
P()
P(int)
----
P()
P(int)
P(double)
----
P()
P(char)
P(boolean)
----
*/
---------------------------------------------------------------------------
13)
class Q 
{
 Q(int i)
 {
  this();
  System.out.println("Q()");
 }
 Q()
 {
  this(20);
  System.out.println("Q(int)");
 }

 public static void main(String[] args) 
 {
  System.out.println("Hello World!");
 }
}
OP:
/*
Compiler error : recursive constructor invocation ,even though we r not creating object
*/
---------------------------------------------------------------------------
14)
class R 
{
 R()
 {
  this(10);
  System.out.println("R()");
 }
 R(int i)
 {
  System.out.println("R(int)");
 }

 /*public static void main(String[] args) 
 {
  System.out.println("Hello World!");
 }*/
}
OP:
Compile successfully,but cant run due to no main method


No comments:

Post a Comment