Methods, Classes, and Objects
Methods, Classes, and Objects
Creating a method
syntax:
modifier returnValueType methodName( )
{
//Method body
}
Methods
Example:
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.
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();
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
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.