Lecture 3
Methods and Constructors
Outcome of the Lecture
Upon completion of this lecture you will be able to
✓ Understand and write methods and constructors
✓ Create Objects using constructors
✓ Access objects via reference variable
✓ Writing and using complete classes
✓ Use of this keyword
Outline of the Presentation
✓ Defining and invoking Methods
✓ main() method
✓ Constructors
✓ Constructing Objects using constructors
✓ Reference variables and reference types
✓ Accessing Object’s data and methods via reference
variables
✓ this keyword
Defining and invoking Methods
Defining and invoking Methods
✓ A method is a collection of statements that are grouped together to perform an operation.
Define a method Invoke a method
return value method formal
modifier type name parameters
int z = max(x, y);
method
public static int max(int num1, int num2) {
header
actual parameters
int result; (arguments)
method
body parameter list
if (num1 > num2)
result = num1;
else
method
result = num2; signature
return result; return value
}
main() method
Special Methods
✓ main() method
✓ constructors
main() method
• Execution starts from main() method
• main is defined as
public static void main(String[] args)
public: so that it may be accessed outside the class by JVM
static: need not to create any instance (object) to call main() by JVM
void: does not return any value to JVM
main: registered name of the method to be called by JVM
String[] args: command line arguments of String type
Constructors
Special Methods
✓ main() method
✓ constructors
Constructors
• Constructors are a special kind of methods that are invoked to construct objects
• Constructors must have the same name as the class itself.
• Constructors do not have a return type—not even void.
• A constructor with no parameters is referred to as a no-argument constructor (parameter
less constructor)
No argument Constructor Parameterized Constructor
Circle() { Circle(double newRadius) {
} radius = newRadius;
}
Constructing Objects using Constructors
✓ Constructors are invoked using the new operator when an object is created.
new ClassName();
Example
new Circle(); // creating object using no argument constructor
new Circle(5.0); // creating object using parameterized constructor
Default Constructor
* A no-arg constructor with an empty body is implicitly declared in the
class, if class is declared without constructors.
* This is called default constructor
* This provided automatically only if no constructors are explicitly
declared in the class.
Reference Variables and Reference Types
✓ A class defines a type called reference type e.g. Circle
✓ Objects are accessed via object reference variables which contains references
to objects
ClassName objectRefVar;
Example:
Circle myCircle;
Creating Objects and assigning reference in single step
ClassName objectRefVar = new ClassName();
Reference variable Create an object
Example:Reference type
Circle myCircle = new Circle();
* If a data field of a reference type does not reference any object, the data field
holds a special literal value, null
Accessing Object’s data and methods via reference variables
✓ Referencing the object’s data:
objectRefVar.data
e.g., myCircle.radius
data field radius is instance variable because it is dependent on a specific instance
✓ Invoking the object’s method:
objectRefVar.methodName(arguments)
e.g., myCircle.getArea()
method getArea() is instance method because it is dependent on a specific instance
Complete Java Program (Circle.java)
public class Circle {
double radius = 1.0;
Circle() {
}
Circle(double newRadius) {
radius = newRadius;
}
double getArea() {
return radius * radius * Math.PI;
}
public static void main(String args[]) {
Circle myCircle = new Circle(10);
System.out.println(“Area is “+myCircle.getArea());
}
}
Variation in Circle.java
If default constructor is not defined explicitly
public class Circle {
• Default constructor will not be double radius = 1.0;
provided implicitly Circle(double newRadius) {
• Can not create instance of circle radius = newRadius;
}
using double getArea() {
return radius * radius * Math.PI;
Circle myCircle = new Circle(); }
public static void main(String args[]) {
Circle myCircle = new Circle(10);
System.out.println(“Area is “+myCircle.getArea());
}
}
this keyword
✓Refers to calling object itself
✓Used to
• To refer to hidden data field of the class
• To enable a constructor to invoke another
constructor of same class (statement that uses the
keyword this appear first in the constructor)
this keyword
class This{
int i;
public This(){
this(5); Invoking parameterized constructor
}
public This(int i){
this.i=i; Referring hidden data member i
}
public void show(){
System.out.println("i = "+i);
}
} public This(int x){ public This(int x){
Equivalent to
public class DemoThis{ x=i; this.x=i;
public static void main(String[] a){ } }
This t1 = new This();
t1.show();
This t2 = new This(20);
t2.show();
}
}