Upload
brent-lester
View
233
Download
0
Embed Size (px)
Citation preview
Java Classes
Chapter 1
2
Chapter Contents
Objects and ClassesUsing Methods in a Java Class• References and Aliases• Arguments and Parameters
Defining a Java Class• Passing Arguments• Constructors• The toString Method• Static Fields and Methods
Packages – The Java Class Library
3
Objects
An object is a program construct that• Contains data• Performs certain actions
The actions are called methods
The actions interact to form the solution to a given problem
4
Classes
A class is a type or kind of object
Objects of the same class have• The same kinds of data• The same methods
A class definition is a general description of• What the object is• What it can do
5
An Example of a Class
Fig. 1-1 An outline of a class
6
Instantiation of the Class
Fig. 1-1 Three instances of the class Automobile
7
Using Methods in a Java Class
Given: jName joe = new Name();
The new operator creates an instance of the class• Invokes the constructor method
Valued methods return a single value
void methods do not return a value
8
Using Methods in a Java Class
Fig. 1-2 A variable that references an object.
9
References and Aliases
Primitive types:byte, short, int, longfloat, double, char, boolean
All other types are reference or class typesString greeting = "Howdy";• greeting is a reference variable
When two variables reference the same instance, they are considered aliases
10
References and Aliases
Fig. 1-3 Aliases of an object
11
Arguments and Parameters
GivenName joe = new Name();joe.setFirst ("Joseph");joe.setLast ("Brown");
"Joseph" and "Brown" are arguments sent to the methods
Invocation of method must have same number of arguments as there are formal parameters in the declaration
12
Defining a Java Class
Given
These are the data fields (instance variables)
Note they are private• They will require accessor and mutator
methods
public class Name
{ private String first; // first nameprivate String last; // last name
< Definitions of methods are here. >
} // end Name
13
Method Definitions
Given
• This is a valued method• Returns a String
Given
• This is a void method
public void setFirst(String firstName)
{ first = firstName; } // end setFirst
public String getFirst()
{ return first; } // end getFirst
14
Naming Methods
Start method name with lowercase letter• Use verb or action phrase
Start class name with uppercase• Use noun or descriptive phrase
Local variables• A variable declared within a method
15
Passing Arguments
Call by value• For primitive type, parameter initialized to
value of argument in call
Call by reference• For a class type, formal parameter is initialized
to the address of the object in the call
16
Passing Arguments
Fig.1-4 a & b The method giveLastNameTo modifies the object passed to it as an argument.
17
Passing Arguments
Fig.1-4 c & d The method giveLastNameTo modifies the object passed to it as an argument.
18
Passing Arguments
Figure 1-5 a & b A method cannot replace an object passed to it as an argument.
19
Passing Arguments
Figure 1-5 c & d A method cannot replace an object passed to it as an argument.
20
Constructors
A method that• Allocates memory for the object• Initializes the data fields
Properties• Same name as the class• No return type, not even void• Any number of formal parameters
21
Constructors
Fig. 1-6 An object (a) after its initial creation; (b) after its reference is lost
22
Static Fields and Methods
A data field that does not belong to any one object
One instance of that data item exists to be shared by all the instances of the class
Also called:static field, static variable, class variable
23
Static Fields and Methods
Fig. 1-7 A static PI versus a non static field
24
Packages
Multiple related can be conveniently grouped into a packageBegin each file that contains a class within the package
package myStuff;
Place all files within a directory• Give folder same name as the package
To use the package, begin the program with
import myStuff.*;
25
The Java Class Library
Many useful classes have already been declared
Collection exists in Java Class Library
Example• The class Math is part of the package java.lang