Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1

2 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

3 E XAMPLE : T HE C AR C LASS 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

4 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 E XAMPLE 1: T HE C AR C LASS

5 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 E XAMPLE 1: T HE C AR C LASS

6 C ONSTRUCTING 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

7 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 C ONSTRUCTING OBJECTS WITH NEW

8 This is often condensed into one line like this: Car c = new Car(); 8 C ONSTRUCTING OBJECTS WITH NEW

9 T HE M EMBER A CCESS S EPARATOR class 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

10 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 = ; 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 T HE M EMBER A CCESS S EPARATOR

11 U SING A C AR OBJECT IN A DIFFERENT CLASS class 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 "; c.speed = 70.0; c.maxSpeed = ; System.out.println(c.licensePlate + " is moving at " + c.speed + " kilometers per hour."); } 11

12 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 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 U SING A C AR OBJECT IN A DIFFERENT CLASS

13 I NITIALIZING F IELDS 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 = ; // 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

14 I NITIALIZING F IELDS For example, $ javac Car.java $ javac CarTest2.java $ java CarTest is moving at 0.0 kilometers per hour. 14


Download ppt "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."

Similar presentations


Ads by Google