0% found this document useful (0 votes)
26 views

UNIT-I (Constructors & This Keyword)

This document discusses constructors in Java. It defines a constructor as a special type of method used to initialize object instances. There are two types of constructors: default constructors that take no parameters, and parameterized constructors that accept parameters. The this keyword can be used to refer to current class instance variables when they have the same name as method parameters, and to call current class methods. The document provides examples to illustrate default constructors, parameterized constructors, resolving ambiguity with this, and invoking current class methods with this.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

UNIT-I (Constructors & This Keyword)

This document discusses constructors in Java. It defines a constructor as a special type of method used to initialize object instances. There are two types of constructors: default constructors that take no parameters, and parameterized constructors that accept parameters. The this keyword can be used to refer to current class instance variables when they have the same name as method parameters, and to call current class methods. The document provides examples to illustrate default constructors, parameterized constructors, resolving ambiguity with this, and invoking current class methods with this.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

UNIT-I

(PART-II)
JAVA ANATOMY
TOPICS:
 JAVA Objects and References
 Constructors
 this Keyword
 Arrays
 Strings and Immutability
 Buffer and Builder Classes
 String Tokenizer
CONSTRUCTORS
Definition : Constructor is a Special Member Function
of a class that is used to create objects of that class.
 It is a special type of method which is used to
initialize the object.
Syntax :
class MyClass
{
MyClass()
{
// Group of Statements
}
}
RULES FOR CREATING CONSTRUCTOR
There are Several rules defined for the constructor.
Those are :
 Constructor name must be the same as its class name
 A Constructor cannot have any return type not even
void.
 A Java constructor cannot be abstract, static, final.
 Constructors can not be Inherited.
TYPES OF CONSTRUCTORS
There are two types of constructors in Java:
1. Default constructor (no-arg constructor)
2. Parameterized constructor
1.Default Constructor : constructor is
called "Default Constructor" when it
doesn't have any parameter.
 If there is no constructor in a class,
Compiler automatically creates a
Default Constructor.
Eg-1:

O/P : Bike is Created.


Eg-2: /* default constructor displays default values
*/

O/P : 0 NULL
0 NULL
Eg-3: /*default constructor that displays the
addition of two numbers*/

O/P : Default Constructor


a+b=30
2. Parameterized Constructor :
 A constructor which has a specific
number of parameters is called a
parameterized constructor.
 The parameterized constructor is
used to provide different values to
the distinct objects.
Eg-1: /* Parameterized constructor Example */

O/P : 111 PVP


222 SIT
Difference between Method and Constructor
Java Constructor Java Method

A constructor is used to initialize the state of A method is used to expose the behavior of an
an object. object.

A constructor must not have a return type. A method must have a return type.

The constructor is invoked implicitly. The method is invoked explicitly.

The Java compiler provides a default The method is not provided by the compiler in
constructor if you don't have any constructor any case.
in a class.

The constructor name must be same as the The method name may or may not be same
class name. as class name.
THIS KEYWORD
Definition : this Keyword is a reference
variable that refers to the current
object.
Usage of java this keyword :
1. this: to refer current class
instance variable :
 The this keyword can be used to
refer current class instance variable.
If there is ambiguity between the
instance variables and parameters,
this keyword resolves the problem of
ambiguity.
Eg-1:Problem with out using this Keyword :
CONT…
O/P : 0 NULL 0.0

In the above example, parameters(formal


arguments) and instance variables are same.
So, we are using this keyword to distinguish
local variable and instance variable.
Eg-2:Problem Solving using this Keyword :
CONT…

O/P : 111, ankit, 5000f


2. this: to invoke current class method :
 You may invoke the method of the
current class by using the this keyword.
 If you don't use the this keyword,
compiler automatically adds this
keyword while invoking the method.
Eg-1: O/P : hello n
hello m
Proving this keyword
 Let's prove that this keyword refers to
the current class instance variable.
 In this program, we are printing the
reference variable and this, output of
both variables are same.
O/P :A5@22b3ea59
A5@22b3ea59

You might also like