Introducing Classes, Objects, and Methods
Introducing Classes, Objects, and Methods
Methods
Class Fundamentals
• A class is a template that defines the form of an
object.
• It specifies both the data and the code that will
operate on that data.
• Java uses a class specification to construct objects.
• Objects are instances of a class.
• The methods and variables that constitute a class are
called members of the class. The data members are
also referred to as instance variables.
The General Form of a Class
• Class is created by using the keyword class.
Defining a Class
object.member
• Thus, the object is specified on the left, and the
member is put on the right.
• For example, to assign the fuelcap variable of
minivan the value 16, use the following statement:
• You should call the file that contains this program
VehicleDemo.java because the main( ) method is in
the class called VehicleDemo, not the class called
Vehicle.
• When you compile this program, you will find that two
.class files have been created, one for Vehicle and one
for VehicleDemo.
• The Java compiler automatically puts each class into its
own .class file.
• It is not necessary for both the Vehicle and the
VehicleDemo class to be in the same source file. You
could put each class in its own file, called Vehicle.java
and VehicleDemo.java.
• minivan’s data is completely separate from the data
contained in sportscar.
How Objects Are Created
the string "In Java, strings are objects." is automatically made into a
String object by Java.
Constructing Strings
Operating on Strings
Returns true if the invoking string
contains the same character sequence
Boolean equals(str) as str.