Top Banner
1
14

1. In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods.

Mar 31, 2015

Download

Documents

Mallory Purser
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1. In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods.

1

Page 2: 1. In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods.

In Java everything is an object or a class (or a piece of one or a collection of several).

Objects send messages to each other by calling methods.

Instance methods belong to a particular object.

Static methods belong to a particular class.

2

Page 3: 1. In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods.

EXAMPLE : THE CAR CLASS Suppose you need to write a traffic

simulation program that watches cars going past an intersection. Each car has:

1. a speed.2. a maximum speed.3. a license plate.

that uniquely identifies it. In traditional programming languages you'd have two floating point and one string variable for each car. With a class you combine these into one thing like this.

3

Page 4: 1. In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods.

class Car {

String licensePlate; // e.g. “Palestine 543 A23"

double speed; // in kilometers

per hour

double maxSpeed; // in kilometers per hour

}

These variables (licensePlate, speed and maxSpeed) are called the member variables, instance variables, or fields of the class.

Fields tell you what a class is and what its properties are.

4

EXAMPLE 1: THE CAR CLASS

Page 5: 1. In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods.

An object is a specific instance of a class with particular values (possibly mutable) for the fields. While a class is a general blueprint for objects, an instance is a particular object.

5

EXAMPLE 1: THE CAR CLASS

Page 6: 1. In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods.

CONSTRUCTING OBJECTS WITH NEW

class Car { String licensePlate; // e.g. “Palestine 543 A23"

double speed; // in kilometers per hour double maxSpeed; // in kilometers per hour

}

To instantiate an object in Java, use the keyword new followed by a call to the class's constructor. Here's how you'd create a new Car variable called c:

Car c; c = new Car();6

Page 7: 1. In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods.

The first word, Car, declares the type of the variable c. Classes are types and variables of a class type need to be declared just like variables that are ints or doubles.

The equals sign is the assignment operator and new is the construction operator.

Finally notice the Car() method. The parentheses tell you this is a method and not a data type like the Car on the left hand side of the assignment. This is a constructor, a method that creates a new instance of a class. You'll learn more about constructors shortly. However if you do nothing, then the compiler inserts a default constructor that takes no arguments.

7

CONSTRUCTING OBJECTS WITH NEW

Page 8: 1. In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods.

This is often condensed into one line like this:Car c = new Car();

8

CONSTRUCTING OBJECTS WITH NEW

Page 9: 1. In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods.

THE MEMBER ACCESS SEPARATORclass Car { String licensePlate; // e.g. “Palestine 543 A23"

double speed; // in kilometers per hour double maxSpeed; // in kilometers per hour

} Once you've constructed a car, you want to do

something with it. To access the fields of the car you use the . separator. The Car class has three fields

1. licensePlate 2. speed 3. maxSpeed

9

Page 10: 1. In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods.

Therefore if c is a Car object, c has three fields as well: c.licensePlate c.speed c.maxSpeed

You use these just like you'd use any other variables of the same type. For instance:

Car c = new Car();c.licensePlate = “Palestine 12546"; c.speed = 70.0; c.maxSpeed = 123.45;

System.out.println(c.licensePlate + " is moving at " + c.speed + "kilometers per hour.");

The separator selects a specific member of a Car object by name.

10

THE MEMBER ACCESS SEPARATOR

Page 11: 1. In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods.

USING A CAR OBJECT IN A DIFFERENT CLASSclass Car { String licensePlate; // e.g. “Palestine 543 A23" double speed; // in kilometers per hour double maxSpeed; // in kilometers per hour }

The next program creates a new car, sets its fields, and prints the result:

class CarTest { public static void main(String args[]) { Car c = new Car(); c.licensePlate = " Palestine 12546 ";c.speed = 70.0;c.maxSpeed = 123.45; System.out.println(c.licensePlate + " is moving at " + c.speed + " kilometers per hour.");

} }

11

Page 12: 1. In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods.

This program requires not just the CarTest class but also the Car class. To make them work together put the Car class in a file called Car.java. Put the CarTest class in a file called CarTest.java. Put both these files in the same directory. Then compile both files in the usual way. Finally run CarTest. For example,

% javac Car.java % javac CarTest.java % java CarTest Palestine 12546 is moving at 70.0 kilometers per hour.

Note that Car does not have a main() method so you

cannot run it. It can exist only when called by other programs that do have main() methods.

12

USING A CAR OBJECT IN A DIFFERENT CLASS

Page 13: 1. In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods.

INITIALIZING FIELDS Fields can (and often should) be initialized when they're

declared, just like local variables.

class Car {

String licensePlate = “ "; // e.g. “Palestine 543 A23"

double speed = 0.0; // in kilometers per hour

double maxSpeed = 123.45; // in kilometers per hour

}

The next program creates a new car and prints it: class CarTest2

{

public static void main(String[] args)

{

Car c = new Car();

System.out.println(c.licensePlate + " is moving at " + c.speed + "kilometers per hour.");

}

}

13

Page 14: 1. In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods.

INITIALIZING FIELDS For example, $ javac Car.java $ javac CarTest2.java $ java CarTest is moving at 0.0 kilometers per hour.

14