Java - Parameter

Java Conceptuel Diagram

About

Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration's parameters in type and order.

Name

The name of a parameter must be unique in its scope. It cannot be the same as the name of another parameter for the same method or constructor, and it cannot be the name of a local variable within the method or constructor.

Shadowing the field

A parameter can have the same name as one of the class's fields. If this is the case, the parameter is said to shadow the field. Shadowing fields can make your code difficult to read and is conventionally used only within constructors and methods that set a particular field. For example, consider the following Circle class and its setOrigin method:

public class Circle {
    private int x, y, radius;
    public void setOrigin(int x, int y) {
        ...
    }
}

The Circle class has three fields: x, y, and radius. The setOrigin method has two parameters, each of which has the same name as one of the fields. Each method parameter shadows the field that shares its name. So using the simple names x or y within the body of the method refers to the parameter, not to the field. To access the field, you must use a qualified name.

Passing Argument

Primitive Data Type

Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost.

Reference data type

Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns:

  • the passed-in reference still references the same object as before.
  • but the values of the object's fields can be changed in the method.

Arbitrary Number of Arguments

You can use a construct called varargs to pass an arbitrary number of values to a method.

You use varargs when you don't know how many of a particular type of argument will be passed to the method. It's a shortcut to creating an array manually (the previous method could have used varargs rather than an array).

To use varargs, you follow the type of the last parameter by an ellipsis (three dots, …), then a space, and the parameter name. The method can then be called with any number of that parameter, including none.

public Polygon polygonFrom(Point... corners) {
    int numberOfSides = corners.length;
    double squareOfSide1, lengthOfSide1;
    squareOfSide1 = (corners[1].x - corners[0].x)*(corners[1].x - corners[0].x) 
			+ (corners[1].y - corners[0].y)*(corners[1].y - corners[0].y) ;
    lengthOfSide1 = Math.sqrt(squareOfSide1);
    // more method body code follows that creates 
    // and returns a polygon connecting the Points
}

You can see that, inside the method, corners is treated like an array. The method can be called either with an array or with a sequence of arguments. The code in the method body will treat the parameter as an array in either case.

You will most commonly see varargs with the printing methods; for example, this printf method:

public PrintStream printf(String format, Object... args)

allows you to print an arbitrary number of objects. It can be called like this:

System.out.printf("%s: %d, %s%n", name, idnum, address);

or like this

System.out.printf("%s: %d, %s, %s, %s%n", name, idnum, address, phone, email);

or with yet a different number of arguments.





Discover More
Code Refactoring
Code - Refactoring

The process of modifying the code structure, without modifying its current behavior, is called Refactoring. Refactoring is a method that search to improve the code quality of a program. code duplication...
Java Conceptuel Diagram
Java - (Generic|Parameterized) type - (Class|Interface|Method) Parametrization

and Integer.class is of type Class Stronger type checks at compile time. Fixing compile-time errors is easier than fixing runtime errors, which can be difficult to find. The compiler can check the type...
Java Conceptuel Diagram
Java - (Method|Functions)

A function that belong to an object is called a methods. A static method is then a sort of function. Methods: operate on an object's internal state and serve as the primary mechanism for object-to-object...
Java Conceptuel Diagram
Java - Annotations

Since Java version 1.5. An annotation is an element of the java programming language (modifier or Metadata tag) that can be associated with: Java classes, interfaces, constructors, methods,...
Java Conceptuel Diagram
Java - Variable

There are several kinds of variables: Member variables in a class—these are called fields. Variables in a method or block of code—these are called local variables. Variables in method declarations—these...



Share this page:
Follow us:
Task Runner