Java Reference
In-Depth Information
ativetoiteration,andhowtooverloadmethods,youreviewtherulesforinvokingmeth-
ods from different contexts.
Declaring and Invoking Instance Methods
You can declare an instance method by minimally specifying a return type name, fol-
lowedbyanidentifierthatnamesthemethod,followedbyaparameterlist,followedby
abrace-delimitedbody. Listing2-9 presentsa Car classwitha printDetails() in-
stance method.
Listing 2-9. Declaring a printDetails() instance method in the Car class
class Car
{
String make;
String model;
int numDoors;
Car(String make, String model)
{
this(make, model, 4);
}
Car(String make, String model, int numDoors)
{
this.make = make;
this.model = model;
this.numDoors = numDoors;
}
void printDetails()
{
System.out.println("Make = "+make);
System.out.println("Model = "+model);
System.out.println("Number of doors = "+numDoors);
System.out.println();
}
public static void main(String[] args)
{
Car myCar = new Car("Toyota", "Camry");
myCar.printDetails();
 
Search WWH ::




Custom Search