Lec 09 ReferencesScopeStatic

Lec 09 ReferencesScopeStatic - FIT1002 Computer Programming...

Info iconThis preview shows pages 1–3. 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 DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: FIT1002 Computer Programming 2009 Module 9 Object References www.monash.edu.au FIT1002 Computer Programming Module 9 - Object References COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University pursuant to Part VB of the Copyright Act 1968 (the Act). The material in this communication may be subject to copyright under the Act. Any further reproduction or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice. The authors acknowledge that material in these modules has been adapted from slides produced by Lewis, DePasquale and Chase for Java Foundations: Introduction to Programming and Data Structures , Addison-Wesley, 2007 Assumed Knowledge Before the beginning of this lecture, students should be familiar with: Declaring variables and assigning values to them Writing small classes containing attributes and methods Instantiating and sending messages to objects Before the beginning of this lecture, students should have read over: Chapter 3.1, 5.6, 5.7 of Lewis et al 2 Objectives Explain what an object reference is Understand the consequences of objects being passed by reference Be able to pass objects to and return objects from methods Explain the concept of privacy leaks and avoid writing code which exhibits this phenomenon Understand the benefit of writing toString methods 3 FIT1002 Computer Programming 2009 Module 9 Object References Revision Primitive Data Type Variables int, float, double, boolean, Declaring a variable of a primitive type reserves space in memory for a single value of that type Associates a name (identifier) to the memory address 223 totalStudents totalStudents int totalStudents = 223; A class is a description or template for making objects Each object is an individual instance with a unique identity 4 public class TestClass { public static void main (String args) { int x = 7; System.out.println(x); change (x); System.out.println(x); } public void change (int x) { System.out.println(x); x = 2; System.out.println(x); } } Passing Primitive Data types as parameters. Trace this code. Do you understand its behaviour? Are you sure? Execute this code to make sure it does what you expected. 5 Object Reference Variables revisited What happens when we declare an object variable? Person aPerson; aPerson aPerson aPerson is called an object reference variable because it can hold memory addresses that refer to an object. The value stored in aPerson is therefore an address (not a Person). Use new to make the instance of the Person to which the address refers....
View Full Document

This note was uploaded on 08/15/2010 for the course FIT 1002 taught by Professor Mylini during the Three '10 term at Monash.

Page1 / 15

Lec 09 ReferencesScopeStatic - FIT1002 Computer Programming...

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

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