Download presentation
Presentation is loading. Please wait.
Published byNatalie Moore Modified over 6 years ago
1
Concepts for this lecture: Class Instance of a class, (an object)
CS100A, Fall 1998, 8 September Concepts for this lecture: Class Instance of a class, (an object) Fields (instance variables) Declaration of class variables Operator new Class methods Reading in Holmes ( ) 6.7 ( ) CS100A, 8 Sept Lecture 3
2
We want boxes (variables) that can contain more than one value, e.g.
Variable: a variable is a named box into which one can place a value of some type. int x We want boxes (variables) that can contain more than one value, e.g. Coordinate c -49 x 25 y 20 CS100A, 8 Sept Lecture 3
3
c1 and c5 are instances of the class; they are objects.
x y 25 Coordinate c1 Coordinate c5 Coordinate is a class. A class is like a type, except that YOU can declare it. c1 and c5 are instances of the class; they are objects. c1.x and c1.y are fields of object c1. x y 21 CS100A, 8 Sept Lecture 3
4
keywords public and class
Declaration in Java of class Coordinate: public class Coordinate { public int x; public int y; } keywords public and class name of the class. Convention: capitalize all words in a class names. E.g. StringBuffer, body of the class, delimited by { } field declarations. Just normal type and class declarations methods and constructors (discussed later) CS100A, 8 Sept Lecture 3
5
Type declaration and class declarations int x; // x Coordinate w; // w
null is a Java constant; it denotes the absence of an object. null CS100A, 8 Sept Lecture 3
6
Creating a new instance of a class: use new:
Coordinate w; // w w= new Coordinate(); null x ? y ? CS100A, 8 Sept Lecture 3
7
Referencing a field of an instance. // w // x x= w.x;
y 5 6 x 3 y 5 3 CS100A, 8 Sept Lecture 3
8
Assigning to a field of an instance
Assigning to a field of an instance. (Later, we’ll investigate good programming practices for referring to and assigning to fields of an instance. // w w.x= 7; // w x 3 y 5 x 7 y 5 CS100A, 8 Sept Lecture 3
9
Assigning one instance to another: creates alias: two names refer to the same object!
h= w; // w // h x 3 y 5 x 7 y 1 x 3 y 5 x 7 y 1 CS100A, 8 Sept Lecture 3
10
Classes can have methods that operate on the fields of the class
public class Coordinate { public int x; public int y; // Set field x to p*p public void setX(int p) { x= p*p; } // Set field y to q public void setY(int q) { y= q; // Return the sum of the squares of the fields public int sumSquares() { return x*x + y*y; CS100A, 8 Sept Lecture 3
11
x 9 y 2 Execution of statement return <expression>
terminates execution of the method (function) in which it appears and “returns” the value of <expression> to the place of call. Example of calls: c.setX(3); c.setY(2); x //Store 85 in s s= c.sumSquares(); x y 2 CS100A, 8 Sept Lecture 3
12
We discussed several concepts concerning a classes:
Definition of a class, including fields and methods, Declaration of a class variable, Creation of a new instance of a class and storing it in a class variable. Referencing and assigning to a field of a class. Assigning a class instance to a class variable, thus creating an alias. Class methods (procedures and functions) We have looked only at the mechanics; we haven’t spend much time on using these things. That will come later. For now, it’s imperative that you learn the mechanics. The demonstrations using CodeWarrior on the Mac should help provide some perspective. CS100A, 8 Sept Lecture 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.