Intro to Java Web-Notes_Part12

Intro to Java - The declaration of the reference variable the instantiation of the Bus object via the new operator and the attachment of the

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: The declaration of the reference variable, the instantiation of the Bus object via the new operator, and the attachment of the reference variable to the Bus object, can be achieved in one statement instead of the two used above: Bus trainSation = new Bus(); An obj ect's data members can be accessed via a reference to that object: trainStation.passengers : 33; The "." after trainStation is a member access operator. It allows access to the contents of the referenced object. In this case we're starting with the trainStation reference , then use the "." member access operator to gain access to the passenger member of the referenced object (we then use the "=" assignment operator to change the passenger data member to 33). Once the above assignment instruction has been performed by the CPU, the visualization of what is in memory (the "memory picture") changes to trainStatiofi: pagsengers % Note that trainStation. passengers is an expression, with a value of 33. Being able to create these simple memory pictures can greatly enhance your comprehension of object-oriented programming. Take out a scrap of paper and draw a memory picture of the objects and reference variables that are in memory after the following instructions have been performed Bus airport = new Bus(); Bus school : new Bus(); Bus downtown : new Bus(); airport.passengers : 5; school.passengers = ll; school.passengers++; downtown.passengers = airport.passengers + 2; airport.passengers -: 1; After the above instructions have been performed, what are the values of the expressions airport. passengers, school. passengers, and downtown. passengers? Click here to see the values. Click here to see my representation of memory. Note that a single object can be referenced by several different reference variables simultaneously. The following code causes all three reference variables to reference the same object -- the object with the value 35 (originally the object held the value 50). The object with the value 17 is left unreferenced. When you assign one reference variable to another, such as in the statement airport 2 school, shown below, the reference (i.e., the "arrow", which is in fact a memory location of the referenced object) gets copied from one reference variable to the other. Bus airport : new Bus(); Bus school : new Bus(); Bus downtown; airpo:t.passengers = 17; school.passengers : 50; airport : school; // change the reference airport, not what it is referencing downtown : school; airpo:t.passengers = 35; Click here to see my representation of memory. Click here to see the values of the expressions airport. passengers, school. passengers, and downtown. passengers. If you are not following the manipulations of the reference variables in the two examples shown above, I have a couple of discussions that you can listen to. The first discussion makes sure that you are familiar with the simple fundamentals of the assignment statement, followed by a discussion of assignments statements and object references (since these are audio/visual files that are fairly large -- three or four megabytes in size). Defining Methods Inside the Objects In addition to holding data values, objects usually also contain instructions to manipulate the data values. These instructions are grouped together in methods. A method is a group of instructions with a name. Here is an updated version of the Bus class -- I've added a method whose instructions add one to the passenger data member. The name of the method is oneMoreAboard. I've also added a method named everyoneOfl. The everyoneOfl method empties the bus of all passengers. class Bus { int passengers; void oneMoreAboard() { passengers++; } void everyoneOff() { passengers = O; } } The instructions of the method can be run by "calling" the methods name, as shown below: Bus school = new Bus(); school.oneMoreAboard(); The oneMorePassenger method is accessed via "school.oneMorePassenger()" , , We This "calls" the method, which causes the instructions of the method to be performed. In this case, there is one instruction, which adds one to the passengers variable: ...
View Full Document

This note was uploaded on 11/17/2010 for the course CIS 1500 taught by Professor Staff during the Summer '08 term at Oakland CC.

Page1 / 2

Intro to Java - The declaration of the reference variable the instantiation of the Bus object via the new operator and the attachment of the

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

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