Body sun = new Body( ); An object is created by the new method The runtime system will allocate enough memory to store the new object If no enough space, the automatic garbage collector will reclaim space from other no longer used objects. If there is still no enough space, then an OutOfMemoryError exception will be thrown No need to delete explicitly | Lecture 3: Object Oriented Programming Object Creation Body sun = new Body( ); An object is created by the new method The runtime system will allocate enough memory to store the new object If no enough space, the automatic garbage collector will reclaim space from other no longer used objects. If there is still no enough space, then an OutOfMemoryError exception will be thrown No need to delete explicitly define a variable sun to refer to a Body object create a new Body object class Body { private long idNum; private String name = “empty”; private Body orbits; private static long nextID = 0; } Constructor constructor is a way to initialize an object before the reference to the object is returned by new has the same name as the class can have any of the same access modifiers as class members similar to methods. A class can have multiple constructors as long as they have different parameter list. Constructors have NO return type. Constructors with no arguments are called no-arg constructors. If no constructor is provided explicitly by the programmer, then the language provides a default no-arg constructor which sets all the fields which has no initialization to be their default values. It has the same accessibility as its class. Sample Class and Constructors class Body { private long idNum; private String name= “empty”; private Body orbits; private static long nextID = 0; Body( ) { idNum = nextID++; } Body(String bodyName, Body orbitsAround) { this( ); name = bodyName; orbits = orbitsAround; } } Assume no any Body object is Assume no any Body object is constructed before: constructed before: Body sun = new Body( ); Body sun = new Body(“Sol”, null); Body earth = new Body(“Earth”, sun); idNum: name: orbits: sun nextID = idNum: name: orbits: sun nextID = idNum: name: orbits: earth nextID = 0 empty null 1 0 Sol null 1 Earth sun 1 2 Usage of this inside a constructor, you can use this to invoke another constructor in the same class. This is called explicit . | Lecture 3: Object Oriented Programming Object Creation Body sun = new Body( ); An object is created by the new method The runtime system will allocate enough memory to store the new object If no enough space, the automatic garbage collector will reclaim space from other no longer used objects. If there is still no enough space, then an OutOfMemoryError exception will be thrown No need to delete explicitly define a variable sun to refer to a Body object create a new Body object class Body { private long idNum; private String name = “empty”; private Body orbits; private static long nextID = 0; } Constructor constructor is a way to initialize an object before the reference to the object is returned by new has the same name as the class can have any of the same access modifiers as class members similar to methods. A class can have multiple constructors as long as they have different parameter list. Constructors have NO return type. Constructors with no arguments are called no-arg .