8.5 Using Arrays in Classes

8.5.1 Using Arrays in User-Defined Classes

An array can be used inside a user-defined class just like any other variable. In particular,

  • an array can be an instance variable.

  • an array can be a parameter to a method.

  • a method can return an array.

  • an array can be a local variable inside a method.

To define a method that takes an array as a parameter, use this syntax:

accessModifier returnType methodName( dataType [ ] arrayName )

The syntax for a method header that returns an array is

accessModifier dataType [ ] methodName( parameterList )

To pass an array as an argument to a method, just use the array name without brackets as the argument value:

Get Java Illuminated, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.