|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The declaration for a method or a constructor declares the number and the type of the arguments for that method or constructor. For example, the following is a method that computes the monthly payments for a home loan, based on the amount of the loan, the interest rate, the length of the loan (the number of periods), and the future value of the loan:This method takes four arguments: the loan amount, the interest rate, the future value and the number of periods. The first three are double-precision floating point numbers, and the fourth is an integer.public double computePayment(double loanAmt, double rate, double futureValue, int numPeriods) { double I, partial1, denominator, answer; I = rate / 100.0; partial1 = Math.pow((1 + I), (0.0 - numPeriods)); denominator = (1 - partial1) / I; answer = ((-1 * loanAmt) / denominator) - ((futureValue * partial1) / denominator); return answer; }As with this method, the set of arguments to any method or constructor is a comma-separated list of variable declarations, where each variable declaration is a type/name pair. As you can see from the body of the
computePaymentmethod, you simply use the argument name to refer to the argument's value.
You can pass an argument of any data type into a method or a constructor. This includes primitive data types, such as doubles, floats, and integers, as you saw in thecomputePaymentmethod, and reference data types, such as classes and arrays. Here's an example of a factory method that accepts an array as an argument. In this example, the method creates a newPolygonobject and initializes it from a list ofPoints (assume thatPointis a class that represents an x, y coordinate):The Java programming language doesn't let you pass methods into methods. But you can pass an object into a method and then invoke the object's methods.public static Polygon polygonFrom(Point[] listOfPoints) { ... }
When you declare an argument to a method or a constructor, you provide a name for that argument. This name is used within the method body to refer to the data.The name of an argument must be unique in its scope. It cannot be the same as the name of another argument for the same method or constructor, the name of a local variable within the method or constructor, or the name of any parameter to a
catchclause within the same method or constructor.An argument can have the same name as one of the class's member variables. If this is the case, the argument is said to hide the member variable. Hiding member variables can make your code difficult to read and is conventionally used only within constructors and methods that set a particular member variable. For example, consider the following
Circleclass and itssetOriginmethod:Thepublic class Circle { private int x, y, radius; public void setOrigin(int x, int y) { ... } }Circleclass has three member variables:x,y, andradius. ThesetOriginmethod accepts two arguments, each of which has the same name as one of the member variables. Each method argument hides the member variable that shares its name. So using the simple namesxorywithin the body of the method refers to the argument, not to the member variable. To access the member variable, you must use a qualified name. See the section Using the this Keywordfor details.
Arguments are passed by value. When invoked, a method or a constructor receives the value of the variable passed in. When the argument is of primitive type, "pass by value" means that the method cannot change its value. When the argument is of reference type, "pass by value" means that the method cannot change the object reference but can invoke the object's methods and modify the accessible variables within the object.To get a better idea of what this means, let's look at a method called
getRGBColorwithin a class calledPen. This method is attempting to return three values by setting the values of its arguments:This simply does not work. Thepublic class Pen { private int redValue, greenValue, blueValue; ... //This method does not work as intended. public void getRGBColor(int red, int green, int blue) { red = redValue; green = greenValue; blue = blueValue; } }red,green, andbluevariables exist only within the scope of thegetRGBColormethod. When that method returns, those variables are gone and any changes to them lost.Let's rewrite the
getRGBColormethod so that it does what was intended. First, we need a new type of object,RGBColor, that can hold the red, green, and blue values of a color in RGB space:Now we can rewritepublic classRGBColor{ public int red, green, blue; }getRGBColorso that it accepts anRGBColorobject as an argument. ThegetRGBColormethod returns the current color of the pen by setting thered,green, andbluemember variables of itsRGBColorargument:The changes made to thepublic class Pen { private int redValue, greenValue, blueValue; ... public void getRGBColor(RGBColoraColor) { aColor.red = redValue; aColor.green = greenValue; aColor.blue = blueValue; } }RGBColorobject within thegetRGBColormethod persist after the method returns, becauseaColoris a reference to an object that exists outside the scope of the method.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.