Download presentation
Presentation is loading. Please wait.
1
Where do objects come from? Objects are instances of classes We instantiate classes: –e.g.new chapter1.Terrarium() –There are three parts to this expression: new chapter1.Terrarium ()
2
new chapter1.Terrarium() ‘new’ is a “reserved word” in Java. This means that the word ‘new’ has a special meaning in the Java language. ‘new’ is the name of an operator whose job it is to create an instance of a given class chapter1.Terrarium is the name of the class we are instantiating. It is a compound name, consisting of a package name (chapter1) and the name of the class’ constructor (Terrarium), separated by a dot ‘.’ A constructor initializes the state of a newly created object. The parentheses delimit the argument list of the constructor call. In this case there are no arguments being passed along to the constructor, so the argument list is empty.
3
Packages A package is an organizational mechanism One analogy: –package::class –area code::phone number A class’ fully qualified name consists of its package name and its (unqualified) class name: –chapter1.Terrarium is a fully qualified class name –Terrarium is an unqualified class name
4
On to Eclipse package lecture; class EcoSystem { public EcoSystem() { }
5
Syntax package lecture; public class EcoSystem { public EcoSystem() { } Package declaration is shown in green:
6
Syntax package lecture; public class EcoSystem { public EcoSystem() { } package is a reserved word:
7
Syntax package lecture; public class EcoSystem { public EcoSystem() { } lecture is the name of the package – you choose this (we’ll cover naming rules and conventions later):
8
Syntax package lecture; public class EcoSystem { public EcoSystem() { } A semicolon ‘;’ marks the end of the declaration:
9
Syntax package lecture; public class EcoSystem { public EcoSystem() { } The class definition is shown in green:
10
Syntax package lecture; public class EcoSystem { public EcoSystem() { } The class definition consists of a header...
11
Syntax package lecture; public class EcoSystem { public EcoSystem() { }... and a body:
12
Syntax package lecture; public class EcoSystem { public EcoSystem() { } The class header consists of an access control modifier...
13
Syntax package lecture; public class EcoSystem { public EcoSystem() { }... the reserved word class...
14
Syntax package lecture; public class EcoSystem { public EcoSystem() { }... and a class name:
15
Syntax package lecture; public class EcoSystem { public EcoSystem() { } The class body begins with an opening brace ‘{’...
16
Syntax package lecture; public class EcoSystem { public EcoSystem() { }... and ends with the matching closing brace ‘}’ :
17
Syntax package lecture; public class EcoSystem { public EcoSystem() { } In this example, the body consists of a single constructor definition:
18
Syntax package lecture; public class EcoSystem { public EcoSystem() { } The constructor definitons consists of a header...
19
Syntax package lecture; public class EcoSystem { public EcoSystem() { }... and a body:
20
Syntax package lecture; public class EcoSystem { public EcoSystem() { } The constructor header consists of an access control modifier...
21
Syntax package lecture; public class EcoSystem { public EcoSystem() { }... the constructor name (which is the same as the class name)...
22
Syntax package lecture; public class EcoSystem { public EcoSystem() { }... and a parameter list:
23
Syntax package lecture; public class EcoSystem { public EcoSystem() { } The constructor body begins with an opening brace ‘{’...
24
Syntax package lecture; public class EcoSystem { public EcoSystem() { }... and ends with the matching closing brace ‘}’ :
25
The constructor The constructor is called after new creates the object. –the object is represented in the memory of the computer –new determines how much memory is needed to represent the object, based on the definition of the class In the example the body of the constructor is empty.
26
Using the constructor Let’s make the constructor do something for us, like make a Terrarium: package lecture; public class EcoSystem { public EcoSystem() { new chapter1.Terrarium(); }
27
Syntax package lecture; public class EcoSystem { public EcoSystem() { new chapter1.Terrarium(); } We have added a statement to the constructor:
28
Syntax package lecture; public class EcoSystem { public EcoSystem() { new chapter1.Terrarium(); } The statement consists of an expression...
29
Syntax package lecture; public class EcoSystem { public EcoSystem() { new chapter1.Terrarium(); }... and a semicolon ‘;’ which marks the end of the statement.
30
Trying it out If we instantiate EcoSystem using DrJava: new lecture.EcoSystem() a new Terrarium object is created!
31
Instantiation Dependency When one object creates an instance of a class, we say that an instantiation dependency exists between the two classes. In UML we show that using a dashed line with an open arrowhead. The arrowhead points to the class being instantiated (the target class). The line start at the class which is doing the instantiation (the source class).
32
Instantiation Dependency in UML
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.