This is one of the comprehensive Java slides I made in 2008.
This covers:
a) Constructors
b) static initializers and blocks
c) non-static initializers and blocks
d) this keyword
e) chaining of constructors
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
Core java day4
1. Constructors
• When you instantiated an object of a class, say A, I used
the syntax A objName=new A();
• The first thing that occurs to you is, what is A()?
• Well, it’s a special type of, well, you may say, methods,
which has the features like….
1. It has no return type not even void
2. It can’t be called without the operator ‘new’
3. As you might have seen but not noticed, it has the same
name as that of the class.
4. It can’t be static.
5. It can be overloaded as any other method. i.e. it can have
sohamsengupta@yahoo.com1
arguments
6. Any method with some return type may have same name
as that of the class, but that’s not the constructor.
2. Execution order of constructors
• Assume the program below…
class A
{
A(){
System.out.println(“From contstructor”);
}
public static void main(String[] args){
A a=new A();
}
}
Output: From Constructor
sohamsengupta@yahoo.com2
3. Before I say more on order of execution, I
spend some time on Types of Constructors
• Constructors are roughly of 2 types… No-argument constructors and
argument-constructors.
• Caution: No-argument constructors are not necessarily the default
constructor. Default constructor is the no-argument constructor
provided by the compiler should the developer give none.
• Once you declare at least one argument-constructor inside your class,
your no-argument default constructor would not be available at all
until you yourself provide your no-argument constructor.
• Copy constructor is a special type of argument constructor where the
argument is an object of the same class. Examples follow on the next
slide
sohamsengupta@yahoo.com3
4. Examples on Constructor
sohamsengupta@yahoo.com4
class A
{
A(int x){
System.out.println(“H”);
}
}
If you write a code like
A objName=new A(); //
Error
Since no-arg constructor is
no more available. Use
A objName=new A(940);
If you must need a no-arg
constructor, you must
either omit arg-ed ones or
simply give one no-arg
constructor
In your code performing as
per your desired taks.
class A
{
int x;
A(A objRef){
this.x=objRef.x;
}
}
It’s a copy constructor. It
takes as argument an
object of the same class and
initializes the instance
value accordingly.
Since it can perform the
job of copying data
members, it’s named
“Copy”
It can, however perform
other tasks. It’s not
mandatory that you always
do the task of copying…. U
can anything even nothing
within a copy constructor.
class A
{
int x,y;
A(int a){
this.x=this.y=a;
}
A(int x,int y){
this.x=x;
this.y=y;
}
}
Thus constructors give the
developer a way to
initialize the data members.
Remember constructors do
not do initialization by
themselves.
5. Now it’s time to know when a constructor is actually called
• Rule: In a class, all the static blocks and
initializers are executed first once and only once in
the order in which they appear in the code, when
the class is loaded into the memory.
• Then non-static blocks, often known as instance
blocks and initializers are executed in the order in
which they appear. They are execute for each
object creation. Instance fields are allocated into
memory with corresponding default values.
• Then constructor is called and inside the
constructor the call to super(<>) must be the first
statement. Well, more on super later
sohamsengupta@yahoo.com5
6. See the example & ponder over the outcome
class B
{
B(){
System.out.println("Constructor");
}
{
System.out.println("Non-static block 1");
sohamsengupta@yahoo.com6
} static{
System.out.println("static block 1");
} static{
System.out.println("static block 2");
}
public static void main(String[] soham){
B b1=new B();
B b2=new B();
}{
System.out.println("Non-static block 2");
}}
static block 1
static block 2
Non-static block 1
Non-static block 2
Constructor
Non-static block 1
Non-static block 2
Constructor
For the first object, when
the class B is loaded into
memory, static blocks are
executed in their order.
This is only once. Then for
each object, first non-static
blocks are executed in
order then the constructor
7. To-Do’s
• Involve in the
Program of Addition
of Complex Numbers
constructors with
arguments.
• Refer to any book for
“chaining” of
constructors.
• In case of chaining,
this() must be the first
statement is a
constructor
• Remember, like static
methods, static blocks
can’t access non-static
data and methods.
• “this” cannot be used
from within a static
context, i.e. a static
method or static block
and no values can be
assigned to “this” as
this is final
sohamsengupta@yahoo.com7