Zoom 1
Zoom 1
Methods
By: Nagesh B S
1
1 Asst. Professor
Dept. of MCA
RNSIT, Bengaluru
Java Programming
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. Thus, a class is
essentially a set of plans that specify how to build an
object.
methods and variables that constitute a class are called
members of the class.
The data members are also referred to as instance
variables.
very simple classes might contain only methods or only
instance variables, most real-world classes contain both.
The General Form of a Class
class classname {
// declare instance variables
type var1;
type var2;
// ...
type varN;
// declare methods
type method1(parameters) {
// body of method
}
type method2(parameters) {
// body of method
}
// ...
type methodN(parameters) {
// body of method
}
}
A well-designed class should define one and only one logical entity.
For example, a class that stores names and telephone numbers will not
normally also store information about the stock market, average rainfall,
sunspot cycles, or other unrelated information.
Example
class Vehicle {
int passengers; // number of passengers
int fuelcap; // fuel capacity in gallons
int mpg; // fuel consumption in miles per gallon
}
A class definition creates a new data type. In this case, the new data type is
called Vehicle.
To create a Vehicle object
To access these variables you will use the dot (.) operator.
object.member
minivan.fuelcap = 16;
class Vehicle {
int passengers; // number of passengers
int fuelcap; // fuel capacity in gallons
int mpg; // fuel consumption in miles per gallon
}
class VehicleDemo {
public static void main(String args[]) {
vehicle minivan = new vehicle();
int range;
// assign values to fields in minivan
minivan.passengers = 7;
minivan.fuelcap = 16;
minivan.mpg = 21;
// compute the range assuming a full tank of gas
range = minivan.fuelcap * minivan.mpg;
System.out.println("minivan can carry " + minivan.passengers + " with a range of " +
range);
} }
Assume the filename as VehicleDemo.java .
It is not necessary for both the Vehicle and the VehicleDemo class to be in
the same source file.
The java compiler put each class in its own file, called Vehicle.java and
VehicleDemo.java, respectively.
To run this program, you must execute VehicleDemo.class.
// This program creates two Vehicle objects.
class Vehicle {
int passengers; // number of passengers
int fuelcap; // fuel capacity in gallons
int mpg; // fuel consumption in miles per gallon
// This class declares an object of type Vehicle.
class TwoVehicles {
public static void main(String args[]) {
Vehicle minivan = new Vehicle();
Vehicle sportscar = new Vehicle();
int range1, range2;
// assign values to fields in minivan
minivan.passengers = 7;
minivan.fuelcap = 16;
minivan.mpg = 21;
// assign values to fields in sportscar
sportscar.passengers = 2;
sportscar.fuelcap = 14;
sportscar.mpg = 12;
// compute the ranges assuming a full tank of gas
range1 = minivan.fuelcap * minivan.mpg;
range2 = sportscar.fuelcap * sportscar.mpg;
System.out.println("Minivan can carry " + minivan.passengers + " with a range of
" + range1);
System.out.println("Sportscar can carry " + sportscar.passengers + " with a range
of " + range2);
}
}
How Objects Are Created
Vehicle minivan = new Vehicle();
First, it declares a variable called minivan of the class type Vehicle.
This variable does not define an object. Instead, it is simply a variable that can refer to
an object.
Second, the declaration creates a physical copy of the object and assigns to minivan
a reference to that object.
This is done by using the new operator.
The new operator dynamically allocates (that is, allocates at run time) memory for an
object and returns a reference to it.
In Java, all class objects must be dynamically allocated.
preceding statement can be rewritten like this to show each step individually:
Methods are subroutines that manipulate the data defined by the class and, in
many cases, provide access to that data.
Can give a method whatever name you please, but…..
Remember that main( ) is reserved for the method that begins execution of
your program. Also ,don’t use Java’s keywords for method names.
void myMeth() {
int i;
for(i=0; i<10; i++) {
if(i == 5) return; // stop at 5
System.out.println();
}
}
Returning a Value
Methods return a value to the calling routine using this form of return:
return value;
This form of return can be used only with methods that have a non-
void return type.
A non-void method must return a value by using this form of return.
class Vehicle {
// assign values to fields in sportscar
int passengers; // number of passengers
sportscar.passengers = 2;
int fuelcap; // fuel capacity in gallons
int mpg; // fuel consumption in miles per gallon sportscar.fuelcap = 14;
// Display the range. sportscar.mpg = 12;
void range() { range1=minivan.range();
return mpg * fuelcap; System.out.print("Minivan can carry " +
} minivan.passengers + “with range of “ +
}
range1+ “Miles”);
class AddMeth {
range2=sportscar.range();
public static void main(String args[]) {
System.out.print(“sportscar can carry " +
Vehicle minivan = new Vehicle();
Vehicle sportscar = new Vehicle(); sportscar.passengers + “with range of “ +