Games and Exercises

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:

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:

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

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

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:

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:

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:


Packt

Share
Published by
Packt

Recent Posts

Top life hacks for prepping for your IT certification exam

I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…

3 years ago

Learn Transformers for Natural Language Processing with Denis Rothman

Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…

3 years ago

Learning Essential Linux Commands for Navigating the Shell Effectively

Once we learn how to deploy an Ubuntu server, how to manage users, and how…

3 years ago

Clean Coding in Python with Mariano Anaya

Key-takeaways:   Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…

3 years ago

Exploring Forms in Angular – types, benefits and differences   

While developing a web application, or setting dynamic pages and meta tags we need to deal with…

3 years ago

Gain Practical Expertise with the Latest Edition of Software Architecture with C# 9 and .NET 5

Software architecture is one of the most discussed topics in the software industry today, and…

3 years ago