Constructors
Constructors
OBJECT ORIENTED
PROGRAMMING I RCS 122
Constructors initialize
object properties
upon object creation.
Constructors
A constructor in is a block of code that initializes
(constructs) the state and value during object
creation.
It is called every time an object with the help of a
new () keyword is created. Even if you haven’t
specified any constructor in the code, the Java
compiler calls a default constructor.
Each time a new object is created, at least one
constructor will be invoked.
The main rule of constructors is that they should
have the same name as the class.
Constructors Cont..
A constructor is syntactically similar to a method, but
there are several differences between the two.
Firstly, a constructor does not have any explicit return
type , not even void.
Secondly, it is invoked implicitly, whereas a method is
not. i.e., it is automatically called; the constructor is
automatically invoked when an object is created using
the new keyword, relieving you from the need to call it
explicitly.
Note: It is called constructor because it constructs the
values at the time of object creation. It is not necessary
Constructors Cont..
In Java, programs are often composed of multiple
classes that work together. While a constructor's
primary role is to initialize the state of a single
object within its class, the interaction between
different classes is crucial. One class might create
an instance of another class, passing data through
the constructor to initialize the new object.
Basic Principles of Multiple Classes
Class Definition Rules
Each class can be defined in the same source file
Only one class can have a public modifier which
will contain the main method.
Constructors Cont..
Consider a program with multiple classes:
package main;
public class Main{
public static void main(String[] args) {
Road.mm("Hello World");
}
}
class Road{
public static void mm(String message){
System.out.println(message);
}
}
Constructors Cont..
Consider another program with multiple classes:
// Example: MultiClassDemo.java
public class MainClass {
public static void main(String[] args) {
HelperClass helper = new HelperClass();
helper.displayMessage();
}
}
class HelperClass {
void displayMessage() {
System.out.println("Message from HelperClass");
}
Member variables
Member variables in Java, also known as fields, are
variables declared within a class but outside any method,
constructor, or block.
They represent the attributes or characteristics of an
object of that class.
There are two main types of member variables:
Instance variables: Each object (instance) of a class has
its own copy of instance variables. Changes made to an
instance variable in one object do not affect the value of
the same variable in other objects. Reinitialized in
methods.
Class variables (static variables): These variables are
Member variables
Example
public class IntegerClass {
// anInteger is an instance variable.
// Each instance (object) of IntegerClass has its own copy of
anInteger.
int anInteger;
Example
Class Demo{
public Demo () {
System.out.println(“This is Text”);
}
}
i. Default Constructor cont..
class Student {
In this this class,
public void printName(String studentName) {
we are not
System.out.println("Student Name: " + studentName); creating any
} constructor, so
compiler
}
provides us a
public class Rucu {// Main class to demonstrate the Student default
class
public static void main(String[] args) { constructor.
Here, 0 and null
Student myStudent = new Student(); // Create a new Student object named
'myStudent’ and use it to call
values are
provided by
myStudent.printName(“Kajala"); // Pass the name “Kajala" as an argument.
default
Student anotherStudent = new Student(); constructor.
anotherStudent.printName("Alice");
ii. Parameterized Constructors
Example
public class Main {
int modelYear; //member variable
String modelName; //member variable
public Main(int year, String name) { //constructor
modelYear = year;
modelName = name;
}
public static void main(String[] args) {
Main myCar = new Main(1969, "Mustang");
System.out.println(myCar.modelYear + " " + myCar.modelName);
}
}
ii. Parameterized Constructors cont..
Example
class Example3 {
private int var; //member variable
public Example3() { //constructor
var = 10;
}
public Example3( int num) {
var = num;
}
public int getValue() {
return var;
}
public static void main (String args[]) {
Example3 obj1 = new Example3();
System.out.println(“Var is:” + obj1.getValue());
Example3 obj2 = new Example3(70);
System.out.println(“Var is:” + obj2.getValue());
this keyword in Java
Uses of ‘this’
Distinguishing instance variables from local
parameters: When a method or constructor
this keyword in Java cont..
Example
class Example {
private int value;
public Example(int value) {
this.value = value; // this.value refers to the instance variable, value
refers to the parameter
}}
Example 2
Class A{
int a=10;
public void display(){
int a=20;
System.out.println(a);
System.out.println(this.a); //output???
ii. Parameterized Constructors cont..
CONTROL STRUCTURES
THANK YOU!