You are on page 1of 30

Methods, Classes, and Objects

Instructor: Mr. Neil A. Basabe, MIT


Methods
A method is a collection of statements that are
grouped together to perform an operation.

Creating a method
syntax:
modifier returnValueType methodName(list of parameters)
{
//Method body
}
Methods
Example:

public static int max(int num1, int num2)


{
int result = 0;
if(num1>num2)
result = num1;
else
result=num2;
return result;
}
Note: A method with a nonvoid return value type is called a function; a method with
a void return type is called a procedure.
Methods
Calling a Method
In creating a method, you give a definition of what the method is to do. To
use a method, you have to call or invoke it. There are two ways to call a
method; the choice is based on whether the method returns a value or not.
If the method returns a value, a call to the method is usually treated as a
value. For example,
int larger = max(3,4);
Calls max(3,4) and assigns the result of the method to the variable larger.

If the methods returns void, a call to the method must be a statement. For
example, the method println returns void. The following call is a statement:

System.out.println(“Welcome to Java!”);
Methods
Passing Parameters
The power of a method is its ability to work with parameters. When calling
a method, you need to provide actual parameters, which must be given in the
same order as their respective formal parameters in the method specification.
This is known as parameter order association. For example, the following
method prints a message n times:
void nPrintln(String message, int n)
{
for(int i=0;i<n;i++)
System.out.println(message);
}
You can use nPrintln(“Hello”, 3) to print “Hello” three times.
Methods
Pass by Value
When invoking a method with parameters, a copy of the value of the actual
parameter is passed to the method. This is referred to as pass by value. The
actual parameters outside the method is not affected, regardless of the changes
made to the formal parameter inside the method.
Overloading Methods
- If two methods or more methods have the same name but different
parameter profiles.
For example,
static double max(double num1, double num2)
{
if(num1 > num2)
return num1;
else
return num2;
}
Methods
static int max(int num1, int num2)
{
if(num1 > num2)
return num1;
else
return num2;
}
static double max(double num1, double num2, double num3)
{
return max(max(num1,num2),num3);
}
Methods
Method Abstraction
Method abstraction is defined as separating the use of a method from its
implementation. The client can use a method without knowing how the method
is implemented. If you decide to change the implementation, the client program
will not be affected provided that you don’t change the method signature. So
the implementation of the method is hidden in a black box from the client.

You have already used the method System.out.print to display a string,


used the method MyInput.readDouble to read a double number from the
keyboard, and used the method max to find the maximum number, but as a
user of these methods, you are not required to know how they are
implemented.
Programming with objects and classes
Declaring and creating objects
In order to declare an object, you must use a variable to represent it(which
is similar to declaring a variable for a primitive data type).
Syntax: ClassName objectName;
Example: Circle myCircle;

To actually create myCircle, you need to use the operator new in order to
tell the computer to create an object for myCircle and allocate appropriate
memory space for it.
Syntax: objectName = new ClassName();
Example: myCircle = new Circle();

Shortcut: ClassName objectName = new ClassName();


Example: Circle myCircle = new Circle();
Programming with objects and classes
Differences between Variables of Primitive Types and Object Types

For a variable of a primitive type, the value is of the primitive type. For a
variable of an object type, the value is a reference to where the object is
located. For this reason, a variable of an object type is referred to as a reference
variable.

Garbage Collection

The Java runtime system detects garbage and automatically reclaims the
space it occupies. This process in know as garbage collection.
Programming with objects and classes
Accessing an object’s data and methods
After an object is created, its data and methods can be accessed using the
following dot notation:
objectName.data – references an object’s data
objectName.method – references an object’s method
For example:
myCircle.radius indicates what the radius of myCircle is
myCircle.findArea() returns the area of myCircle

The data field radius is referred to as an instance variable because it is


dependent on a specific instance. For the same reason, the method findArea is
referred to as an instance method, because you can only invoke it on a specific
instance.
Programming with objects and classes
Constructors
Java enables you to define a special method in the class, known as the
constructor, that can be used to initialize an object’s data. The constructor has
exactly the same name as the defining class. Like methods constructors, can be
overloaded, making it easier to construct objects with different initial data
Values. Example:
public class Circle
{ double radius=1.0;
//construct a circle with the specified radius
public Circle(double r){radius=r;}
//construct a circle with the default radius
public Circle(){radius=1.0;}
}
Programming with objects and classes
Passing objects to methods
Just as you can pass the parameters of primitive types to methods, you can
also pass the parameters of an object types to methods. The following example
passes the myCircle object as an argument to the method printCircle();

Class TestPassingObject{
public static void main(String args[]) {
Circle myCircle = new Circle(5.0);
printCircle(myCircle);}
public static void printCircle(Circle c){
System.out.println(“The area of the circle of radius ”
+c.getRadius() + “ is ” + c.findArea()); }
}
Programming with objects and classes
There are important differences between passing a value of primitive
types and passing objects.

• Passing a variable of a primitive type means that the value of the


variable is passed to a formal parameter. Changing the value of
local parameter inside the method does not affect the value of the
variable outside the method.
• Passing an object means that the reference of the object is passed
to the formal parameter. Any changes to the local object that occur
inside the method body will affect the original object that was
passed as the argument.
Programming with objects and classes
Programming with objects and classes
Programming with objects and classes
Programming with objects and classes
Programming with objects and classes
Building Classes
Building Classes
Building Classes
Building Classes
Building Classes
Building Classes
Building Classes
Building Classes
Building Classes
Building Classes
Building Classes

You might also like