3 min read

In this article by Shishira Bhat and Ravi Wray, authors of the book, Learn Java in 7 days, we will study the following concepts:

  • Making an object as the return type for a method
  • Making an object as the parameter for a method

(For more resources related to this topic, see here.)

Let’s start this article by revisiting the reference variablesand custom data types:

Learn Java in 7 days

In the preceding program, p is a variable of datatype,Pen. Yes! Pen is a class, but it is also a datatype, a custom datatype. The pvariable stores the address of the Penobject, which is in heap memory. The pvariable is a reference that refers to a Penobject.

Now, let’s get more comfortable by understanding and working with examples.

How to return an Object from a method?

In this section, let’s understand return types. In the following code, methods returnthe inbuilt data types (int and String), and the reason is explained after each method, as follows:

int add ()
{
int res = (20+50);
return res;
}

The addmethod returns the res(70) variable, which is of the int type. Hence, the return type must be int:

String sendsms ()
{
String msg = "hello";
return msg;
}

The sendsmsmethod returns a variable by the name of msg, which is of the String type. Hence, the return type is String.

The data type of the returning value and the return type must be the same.

In the following code snippet, the return type of the givenPenmethod is not an inbuilt data type. However, the return type is a class (Pen) Let’s understand the following code:

Learn Java in 7 days

The givePen ()methodreturns a variable (reference variable) by the name of p, which is of the Pen type. Hence, the return type is Pen:

Learn Java in 7 days

In the preceding program, tk is a variable of the Ticket type. The method returns tk; hence, the return type of the method is Ticket.

A method accepting an object (parameter)

After seeing how a method can return an object/reference, let’s understand how a method can take an object/reference as the input,that is, parameter.

We already understood that if a method takes parameter(s), then we need to pass argument(s).

Example

Learn Java in 7 days

In the preceding program,the method takestwo parameters,iandk. So, while calling/invoking the method, we need to pass two arguments, which are 20.5 and 15.

The parameter type andthe argument type must be the same.

Remember thatwhen class is the datatype, then object is the data.

Consider the following example with respect toa non-primitive/class data type andthe object as its data:

Learn Java in 7 days

In the preceding code, the Kid class has the eat method, which takes ch as a parameter of the Chocolatetype, that is,the data type of ch is Chocolate, which is a class. When class is the data type then the object of that class is an actual data or argument. Hence,new Chocolate() is passed as an argument to the eat method.

Let’s see one more example:

Learn Java in 7 days

The drink method takes wtr as the parameter of the type,Water, which is a class/non-primitive type; hence, the argument must be an object of theWater class.

Summary

In this article we have learned what to return when a class is a return type for a method and what to pass as an argument for a method when a class is a parameter for the method. 

Resources for Article:


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here