Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 110

Passing Constructor and Method Parameters

Оглавление

Variables passed to a constructor or method are called constructor parameters or method parameters, respectively. These parameters are like local variables that have been pre-initialized. The rules for initializing constructor and method parameters are the same, so we focus primarily on method parameters.

In the previous example, check is a method parameter.

public void findAnswer(boolean check) {}

Take a look at the following method checkAnswer() in the same class:

public void checkAnswer() { boolean value; findAnswer(value); // DOES NOT COMPILE }

The call to findAnswer() does not compile because it tries to use a variable that is not initialized. While the caller of a method checkAnswer() needs to be concerned about the variable being initialized, once inside the method findAnswer(), we can assume the local variable has been initialized to some value.

OCP Oracle Certified Professional Java SE 17 Developer Study Guide

Подняться наверх