260 likes | 355 Views
CSE115: Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu. Phones off & away Name signs out. Agenda. Announcements Cell phones / laptops off & away / Name signs out FAIR WARNING: First exam on Wednesday, Feb 15
E N D
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu
Phones off & away Name signs out
Agenda • Announcements • Cell phones / laptops off & away / Name signs out • FAIR WARNING: First exam on Wednesday, Feb 15 • material up to Friday, Feb 10 / Mon Feb 13 is review • Last time • Syntax review (quick) / Demo • Relationships • in model and code • first relationship: composition • Today • Lifetime/Scope • process memory • Unified Modeling Language (UML) • composition
Relationships in model and code • relationships exist between objects in problem domains • want to capture those relationships in our models and express them in our code
Composition • A whole-part relationship (e.g. Dog-Tail) • Whole and part objects have same lifetime • when whole is created, it has its parts • when whole is destroyed, parts go away too
whole/part creation in code • Whole creates instance of part in its constructor • In Java code, involves 3 changes to whole class: • Declaration of variable of part type • Instantiation of part class in whole class constructor • Assignment of new part instance to variable
Variables’ properties • name • location • type • value (contents) • scope • lifetime
Variable scope • The scope of a variable is the part of a program where a variable declaration is in effect. • Variables declared in different ways have different scope.
Scope of a local variable • A variable declared within a constructor (method) is called a local variable. • The scope of a local variable is from the point of the declaration to the end of the method body.
Scope of an instance variable • A variable declared within a class but outside of any method is called an instance variable. • The scope of an instance variable is the entire class body.
Variables’ properties • name • location • type • value (contents) • scope • lifetime
Lifetime of a variable • The period of time during execution of a program that the variable exists in memory. • This is a dynamic property (one relating to runtime)
Lifetime of a local variable • A local variable comes into existence when a method is called, and disappears when the method is completed.
Lifetime of an instance variable • Instance variables are created when a class is instantiated. • Each object has its own set of instance variables. • Instance variables persist as long as their objects persist • as far as we know right now, objects persist until the end of the runtime of the program.
Memory organization Process A Process B Process C
Memory organization Process A Process B Process C STATIC SEGMENT RUNTIME STACK FREE/AVAILABLE MEMORY HEAP dynamically allocated memory
whole/part creation in code • Whole creates instance of part in its constructor • In Java code, involves 3 changes to whole class: • Declaration of variable of part type • Instantiation of part class in whole class constructor • Assignment of new part instance to variable
whole/part creation in code(elaborated) • Whole creates instance of part in its constructor • In Java code, involves 3 changes to whole class: • Declaration of instance variable of part type • Instantiation of part class in whole class constructor • Assignment of new part instance to instance variable
Important points about composition • Whole has responsibility for creating its parts (which is why instantiation of parts happens in constructor of whole). • Whole can communicate with parts. This is why an instance variable is declared: to establish a name for the newly created object.
Class members:(instance) methods & instance variables • Any class member (method or variable declared in the class body, but not inside a method) must have an access control modifier.
Access Control Modifiers • “public” – the member can be accessed from outside the class • “private” – the member can be access only from inside the class
Class members:(instance) methods & instance variables • Our rule: • methods are public • instance variables are private. • Later in semester we will justify this rule (one we know a little more about the issues involved)
Instance variable declaration • An instance variable declaration consists of: • an access control modifier • a type • a name • a terminating ‘;’
Dog – Tail exampleexpressing it in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } }