Chapter3_2-ObjectsAndInstanceAtributes

The syntax for instantiating an object is new

Info iconThis preview shows page 1. Sign up to view the full content.

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: variableName ; • Step 2: Next, create the object that you refer to. The syntax for instantiating an object is: new ClassName(); • Step 3: Finally, initialize the instance variable declared in 1 by assigning the newly created object to the instance variable. Just as with variable assignment or initialization. The syntax for initializing an object to an instance variable is: variableName = new ClassName(); • The three steps 1,2 and 3 may be combined within the same statement as following (declaration statement with initial value): ClassName variableName = new ClassName(); Page 6 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Object Creation A. The instance A. The instance A. variable is allocated variable is allocated in memory. in memory. crs Object: Course B. The object is B. The object is B. A A created created courseCode B B Course crs; C. The reference of the C. The reference of the C. crs = studentName new Course ( ) ; object created in B is object created in B is assigned to the variable. assigned to the variable. crs Object: Course C C studentName courseCode Code Page 7 State of Memory Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Objects and Instance variables • Once the...
View Full Document

This document was uploaded on 03/03/2014.

Ask a homework question - tutors are online