Java using object as parameter

Passing Information to a Method or a Constructor

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:

public double computePayment( double loanAmt, double rate, double futureValue, int numPeriods) < double interest = rate / 100.0; double partial1 = Math.pow((1 + interest), - numPeriods); double denominator = (1 - partial1) / interest; double answer = (-loanAmt / denominator) - ((futureValue * partial1) / denominator); return answer; >

This method has four parameters: 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. The parameters are used in the method body and at runtime will take on the values of the arguments that are passed in.

Note: 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.

Parameter Types

You can use any data type for a parameter of a method or a constructor. This includes primitive data types, such as doubles, floats, and integers, as you saw in the computePayment method, and reference data types, such as objects and arrays.

Читайте также:  Scroll function in css

Here’s an example of a method that accepts an array as an argument. In this example, the method creates a new Polygon object and initializes it from an array of Point objects (assume that Point is a class that represents an x, y coordinate):

public Polygon polygonFrom(Point[] corners) < // method body goes here >

Note: If you want to pass a method into a method, then use a lambda expression or a method reference.

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);
System.out.printf("%s: %d, %s, %s, %s%n", name, idnum, address, phone, email);

or with yet a different number of arguments.

Parameter Names

When you declare a parameter to a method or a constructor, you provide a name for that parameter. This name is used within the method body to refer to the passed-in argument.

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.

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:

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. This will be discussed later in this lesson in the section titled «Using the this Keyword.»

Passing Primitive Data Type Arguments

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. Here is an example:

public class PassPrimitiveByValue < public static void main(String[] args) < int x = 3; // invoke passMethod() with // x as argument passMethod(x); // print x to see if its // value has changed System.out.println("After invoking passMethod, x codeblock"> 
After invoking passMethod, x = 3

Passing Reference Data Type Arguments

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. However, the values of the object's fields can be changed in the method, if they have the proper access level.

For example, consider a method in an arbitrary class that moves Circle objects:

public void moveCircle(Circle circle, int deltaX, int deltaY) < // code to move origin of circle to x+deltaX, y+deltaY circle.setX(circle.getX() + deltaX); circle.setY(circle.getY() + deltaY); // code to assign a new reference to circle circle = new Circle(0, 0); >

Let the method be invoked with these arguments:

Inside the method, circle initially refers to myCircle . The method changes the x and y coordinates of the object that circle references (that is, myCircle ) by 23 and 56, respectively. These changes will persist when the method returns. Then circle is assigned a reference to a new Circle object with x = y = 0 . This reassignment has no permanence, however, because the reference was passed in by value and cannot change. Within the method, the object pointed to by circle has changed, but, when the method returns, myCircle still references the same Circle object as before the method was called.

Источник

Java Object as Parameter

Objects, like primitive types, can be passed as parameters to methods in Java. When passing an object as a parameter to a method, a reference to the object is passed rather than a copy of the object itself. This means that any modifications made to the object within the method will have an impact on the original object.

The general form to demonstrate "object as parameter" in Java is as follows:

public class MyClass < // Fields or attributes private int attribute1; private String attribute2; private double attribute3; // Constructor public MyClass(int attribute1, String attribute2, double attribute3) < this.attribute1 = attribute1; this.attribute2 = attribute2; this.attribute3 = attribute3; >// Method with object as parameter public void myMethod(MyClass obj) < // block of code to define this method > // More methods >

An object belonging to the "MyClass" class is used as a parameter in the myMethod() function. This enables the method to operate on the passed object and access its attributes and methods.

Java objects as parameters example

Consider the following program demonstrating the object as parameters in Java:

public class MyClass < private int attribute1; private String attribute2; private double attribute3; // Constructor public MyClass(int attribute1, String attribute2, double attribute3) < this.attribute1 = attribute1; this.attribute2 = attribute2; this.attribute3 = attribute3; >// Method with object as parameter public void myMethod(MyClass obj) < System.out.println("Attribute 1: " + obj.attribute1); System.out.println("Attribute 2: " + obj.attribute2); System.out.println("Attribute 3: " + obj.attribute3); >public static void main(String[] args) < MyClass myObject1 = new MyClass(10, "Hello", 3.14); MyClass myObject2 = new MyClass(20, "World", 6.28); // Call the method with object as parameter myObject1.myMethod(myObject2); > >
Attribute 1: 20 Attribute 2: World Attribute 3: 6.28

The class MyClass in this code has three attributes labeled attribute1, attribute2, and attribute3 and a constructor that accepts three parameters. The class also includes a method called myMethod, which accepts as a parameter an object of the same class MyClass. The attributes of the passed object are printed using System.out.println() statements within the method. In the main() method, two MyClass objects are constructed using the constructor, and then the myMethod() method of the first object is invoked with the second object as a parameter. This results in the output of the attributes of the second object to the console.

Before closing the discussion on "object as parameters" in Java, I wanted to include another example, which is given below, that might help you more with the topic.

public class ObjectAsParameterExample < public static void main(String[] args) < MyClass myObject1 = new MyClass("Edwin", 32); MyClass myObject2 = new MyClass("William", 29); // Call the method that takes a MyClass object as a parameter printPersonInfo(myObject1); // Call the method that takes two MyClass objects as parameters printBothPersonsInfo(myObject1, myObject2); > // Method that takes a MyClass object as a parameter public static void printPersonInfo(MyClass person) < System.out.println("Name: " + person.getName()); System.out.println("Age: " + person.getAge()); >// Method that takes two MyClass objects as parameters public static void printBothPersonsInfo(MyClass myObject1, MyClass myObject2) < System.out.println("\n---Person 1---"); System.out.println("Name: " + myObject1.getName()); System.out.println("Age: " + myObject1.getAge()); System.out.println("\n---Person 2---"); System.out.println("Name: " + myObject2.getName()); System.out.println("Age: " + myObject2.getAge()); >> class MyClass < private String name; private int age; public MyClass(String name, int age) < this.name = name; this.age = age; >public String getName() < return name; >public int getAge() < return age; >>
Name: Edwin Age: 32 ---Person 1--- Name: Edwin Age: 32 ---Person 2--- Name: William Age: 29

The printPersonInfo() and printBothPersonsInfo() methods of the ObjectAsParameterExample class accept MyClass objects as parameters. The printPersonInfo() method prints a single person's name and age, whereas the printBothPersonsInfo() method prints the names and ages of two people.

The constructor is used to create two MyClass objects in the main() method, and then the printPersonInfo() method is called to print the information of the first object. Finally, the printBothPersonsInfo() method is invoked with both objects as parameters to print both objects' information.

Liked this article? Share it!

Источник

Оцените статью