Methods and Parameters

Objects of the previously defined Point class are exposed to manipulation by any code that has a reference to a Point object, because its fields are declared public. The Point class is an example of the simplest kind of class. Indeed, some classes are this simple. They are designed to fit purely internal needs for a package (a group of cooperating classes) or to provide simple data containers when those are all you need.

The real benefits of object orientation, however, come from hiding the implementation of a class behind operations performed on its data. Operations of a class are declared via its methodsinstructions that operate on an object's data to obtain results. Methods access internal implementation details that are otherwise hidden from other objects. Hiding data behind methods so that it is inaccessible to other objects is the fundamental basis of data encapsulation.

If we enhance the Point class with a simple clear method
(to clear, or reset the coordinates to zero), it might look like this:

public void clear()
{
x = 0.0;
y = 0.0;
}



The clear method has no parameters, hence the empty () pair after its name; clear is declared void because it does not return any value. Inside a method, fields and other methods of the class can be named directlywe can simply say x and y without an explicit object reference.

Invoking a Method
Objects in general do not operate directly on the data of other objects, although, as you saw in the Point class, a class can make its fields publicly accessible. Well-designed classes usually hide their data so that it can be changed only by methods of that class.

To invoke a method, you provide an object reference to the target object and the method name, separated by a dot. Arguments are passed to the method as a comma-separated list of values enclosed in parentheses. Methods that take no arguments still require the parentheses, with nothing between them.

A method can return only a single value as a result. To return more than one value from a method, you must create an object whose purpose is to hold return values in a single unit and then return that object.

When a method is invoked, the flow of execution leaves the current method and starts executing the body of the invoked method. When the invoked method has completed, the current method continues execution with the code after the method invocation. When we start executing the body of the method, the object that was the target of the method invocation is now the current or receiver object, from the perspective of that method. The arguments passed to the method are accessed via the parameters the method declared.

Here is a method called distance that's part of the Point class shown in previous examples. The distance method accepts another Point object as a parameter, computes the Euclidean distance between itself and the other point, and returns a double-precision floating-point result:

public double distance(Point that)
{
double xdiff = x - that.x;
double ydiff = y - that.y;
return Math.sqrt(xdiff * xdiff + ydiff * ydiff);
}

The return statement causes a method to stop executing its body and return execution to the invoking method. If an expression is part of the return statement then the value of that expression is returned as the value of the method invocation. The type of the expression must be compatible with the return type defined for the method. In the example we use the sqrt method of the Math library class to calculate the square root of the sum of the squares of the differences between the two x and y coordinates.

Based on the lowerLeft and upperRight objects created previously, you could invoke distance this way:

double d = lowerLeft.distance(upperRight);

Here upperRight is passed as an argument to distance, which sees it as the parameter that. After this statement executes, the variable d contains the Euclidean distance between lowerLeft and upperRight.

The this Reference
Occasionally, the receiving object needs to know its own reference. For example, the receiving object might want to add itself to a list of objects somewhere. An implicit reference named this is available to methods, and this is a reference to the current (receiving) object. The following definition of clear is equivalent to the one just presented:

public void clear()
{
this.x = 0.0;
this.y = 0.0;
}

You usually use this as an argument to other methods that need an object reference. The this reference can also be used to explicitly name the members of the current object. Here's another method of Point named move, which sets the x and y fields to specified values:

public void move(double x, double y)
{
this.x = x;
this.y = y;
}

The move method uses this to clarify which x and y are being referred to. Naming the parameters x and y is reasonable, because you pass x and y coordinates to the method. But then those parameters have the same names as the fields, and therefore the parameter names hide the field names. If we simply wrote x= x we would assign the value of the x parameter to itself, not to the x field as required. The expression this.x refers to the object's x field, not the x parameter of move.

Static or Class Methods
Just as you can have per-class static fields, you can also have per-class static methods, often known as class methods. Class methods are usually intended to do operations specific to the class itself, usually on static fields and not on specific instances of that class. Class methods are declared using the static keyword and are therefore also known as static methods.

As with the term "field", the word "method" generally means a per-object method, although the term non-static method is sometimes used for clarity.

Why would you need static methods? Consider the Sony Walkman factory again. The record of the next serial number to be assigned is held in the factory, not in every Walkman. A method that returned the factory's copy of the next available serial number would be a static method, not a method to operate on specific Walkman objects.

The implementation of distance in the previous example uses the static method Math.sqrt to calculate a square root. The Math class supports many methods that are useful for general mathematical manipulation. These methods are declared as static methods because they do not act on any particular instance of the Math class; instead, they group a related set of functionality in the class itself.

A static method cannot directly access non-static members. When a static method is invoked, there's no specific object for the method to operate on, and so no this reference. You could work around this by passing an explicit object reference as an argument to the static method. In general, however, static methods perform class-related tasks and non-static methods perform object-related tasks. Asking a static method to work on object fields is like asking the Walkman factory to change the serial number of a Walkman hanging on the belt of a jogger in Golden Gate Park.

We Are Founder..