Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012
Color Templates TALENTSPRINT | © Copyright The content in this presentation is aimed at teaching learners to: Classes and Objects Define Constructor Explain the need for Constructor Create Constructors and Parameterized Constructors Use this keyword
Color Templates TALENTSPRINT | © Copyright Classes and Objects Using Objects Dice diceOne = new Dice(); diceOne.roll(); int value = diceOne.getFaceValue(); Constructor
Color Templates TALENTSPRINT | © Copyright Example : class Car{ public Car(){ // this is constructor... } Classes and Objects Constructor Is a function that is implicitly invoked when a new object is created. Performs the necessary actions in order to initialize the object. Is a special function with the same name as the class.
Color Templates TALENTSPRINT | © Copyright Classes and Objects ProgramJVM Memory class Car{ String name; public Car(){ name=My Car; } class MainClass{ public static void main(String args[]){ Car c ; c = new Car(); } Stack memory Heap memory Reference variable (Declaration) Reference variable (Declaration) Allocates memory for the object (for Instantiation) Allocates memory for the object (for Instantiation) Calls the constructor Constructor of the Car Class c Name My Car
Color Templates TALENTSPRINT | © Copyright Classes and Objects Constructor Associating a variable name with an object type. Is done by the new keyword, a JAVA operator that creates the object. A call to the constructor follows this new operator. This in turn, initializes the new object. Declaration Instantiation Initialization
Color Templates TALENTSPRINT | © Copyright Classes and Objects ProgramJVM Memory class Car{ String name; public Car(){ name=My First Car; } class MainClass{ public static void main(String args[]){ Car c1,c2; c1 = new Car(); c2 = new Car(); } Stack memory Heap memory c1 c2 Does it sound meaningful to have the same name for both the cars: My First Car? Name My First Car Name My First Car
Color Templates TALENTSPRINT | © Copyright Classes and Objects ProgramJVM Memory class Car{ String name; public Car(String carName){ name=carName; } class MainClass{ public static void main(String args[]){ Car c1,c2; c1 = new Car(My First Car); c2 = new Car(My Second Car); } Stack memory Heap memory c1 c2 Parameterized Constructor Name My Second Car Name My First Car
Color Templates TALENTSPRINT | © Copyright Classes and Objects ProgramJVM Memory class Car{ String name; public Car(){ name=My First Car; } public Car(String carName ){ name=carName; } class MainClass{ public static void main(String args[]){ Car c 1,c2; c1 = new Car(); c 2 = new Car(My Second Car); } Stack memory Heap memory c1 c2 Name My Second Car Name My First Car It is possible for a class to have more than one constructor?
Color Templates TALENTSPRINT | © Copyright Classes and Objects ProgramJVM Memory class Car{ String name; public Car(){ name=My Car; } class MainClass{ public static void main(String args[]){ Car c 1,c2; c1 = new Car(); c2 = c1; } Stack memory Heap memory c1 c2 NameMy Car
Color Templates TALENTSPRINT | © Copyright Classes and Objects ProgramJVM Memory class Car{ String name; public Car(){ name=My Car; } class MainClass{ public static void main(String args[]){ Car c 1,c2; c 1= new Car(); c 2= new Car(); c2=c1; } Stack memory Heap memory c1 c2 NameMy CarNameMy Car Can a class perform an action on itself?
Color Templates TALENTSPRINT | © Copyright Classes and Objects Program class Car{ String name; public Car(){ name=My First Car; } public Car(String name ){ this.name=name; } class MainClass{ public static void main(String args[]){ Car c 1,c2; c1 = new Car(); c 2 = new Car(My Second Car); } Refers to the current object
Color Templates TALENTSPRINT | © Copyright Classes and Objects this keyword Is a reference to the current object within an instance method or constructor. Explicit constructor invocation -- call another constructor in the same class. Example for explicit constructor invocation : public class MyClass { private int x, y, a, b; public MyClass() { this(0, 0, 0, 0); } public MyClass(int a, int b) { this(0, 0, a, b); } public MyClass(int x, int y, int a, int b) { this.x = x; this.y = y; this.a = a; this.b = b; }... }
Color Templates TALENTSPRINT | © Copyright Classes and Objects Will this work? Class Test{ // no constructor int value=10; void display(){ System.out.println(Value is : +value); } class MainClass{ public static void main(String args[]){ Test test = new Test(); // creating object with constructor Test() test.display(); }
Color Templates TALENTSPRINT | © Copyright Classes and Objects What if no constructor is defined in a class ? At the time of compilation the Java Compiler will insert an empty Definition Of Default Constructor to the java class and then it will compile the program. The empty Definition Of Default Constructor of any class will be as follows: public class-name(){}
Color Templates TALENTSPRINT | © Copyright Classes and Objects Create the following class: Class Test{ int sum(int valueOne, int valueTwo){ return valueOne + valueTwo; } Write a main method that instantiates this class, calls sum method and prints the return value. Compile and run the program. TRY IT EXERCISE
Color Templates TALENTSPRINT | © Copyright Classes and Objects