Lec04_InstantiationModifiersEnums_2per

Lec04_InstantiationModifiersEnums_2per - 1 EECS EECS 285...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 EECS EECS 285 285 EECS285 Lecture 04 Object Instantiation, Modifiers, and Enumerated Types Van der Linden Ch. 5-6 2 Andrew M. Morgan EECS EECS 285 285 Constructors • A class may have one or more constructor (aka "ctor") functions • Ctor is called once per object when it is initially created • Ctors are unique methods because they: – Are named exactly the same as the class – Have no return type at all (not even void) • A ctor will be called at every object creation • Default ctor: A ctor with no input parameters – Implicit Default Ctor: Provided by the compiler – no statements in body – Explicit Default Ctor: Written by the class programmer to perform some specific functionality • Value ctors: Ctors that take input via one or more parameters • NOTE: The implicit default ctor only exists when no other ctors are specified by the class programmer!! 2 3 Andrew M. Morgan EECS EECS 285 285 Object Instantiation • Reminder – All object declarations in Java only declare a reference to an object • Object reference declarations are automatically initialized to null • New objects must be instantiated using the "new" operator • When an object is instantiated, the following sequence occurs – Memory for the object is allocated – All bytes of the allocated memory is cleared (set to 0) – Ctor is called (but statements in ctor not executed yet!) • First thing that happens in ctor: Parent class ctor is called • Explicit initializers are performed • Statements in ctor method are executed • Object is now "ready for use" 4 Andrew M. Morgan EECS EECS 285 285 Implicit Default Constructor Example public class InitializersClass { int x = 4; double y; char z = '+'; void printValues() { System.out.println("x: " + x + " y: " + y + " z: " + z); } } public class InitializeDemo { public static void main(String args) { InitializersClass initObj; //Ref only! initObj = new InitializersClass(); System.out.println("Initial vals: "); initObj.printValues(); } } x and z have been given explicit initializers, and will be assigned to their respective values before any ctor statements are executed y has no explicit initializer, but remember, all object memory is cleared , so y will default to 0.0 initObj is created using the implicit default ctor here. Initial vals: x: 4 y: 0.0 z: + 3 5 Andrew M. Morgan EECS EECS 285 285 Value Ctor Example (1 of 2) public class InitializersClass { int x = 4; double y; char z = '+'; InitializersClass(double denom) { y = x / denom; } void printValues() { System.out.println("x: " + x + " y: " + y + " z: " + z); } } public class InitializeDemo { public static void main(String args) { InitializersClass initObj; //Ref only!...
View Full Document

{[ snackBarMessage ]}

Page1 / 13

Lec04_InstantiationModifiersEnums_2per - 1 EECS EECS 285...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online