Passing Constructor and Method Parameters – Building Blocks – 1Z0-829 Study Guide

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.

Defining Instance and Class Variables

Variables that are not local variables are defined either as instance variables or as class vari-ables. An instance variable, often called a field, is a value defined within a specific instance of an object. Let’s say we have a Person class with an instance variable name of type String. Each instance of the class would have its own value for name, such as Elysia or Sarah.

Two instances could have the same value for name, but changing the value for one does not modify the other.

On the other hand, a class variable is one that is defined on the class level and shared among all instances of the class. It can even be publicly accessible to classes outside the class and doesn’t require an instance to use. In our previous Person example, a shared class variable could be used to represent the list of people at the zoo today. You can tell a vari-able is a class variable because it has the keyword static before it. You learn about this in Chapter 5. For now, just know that a variable is a class variable if it has the static key-word in its declaration.

Instance and class variables do not require you to initialize them. As soon as you declare these variables, they are given a default value. The compiler doesn’t know what value to use and so wants the simplest value it can give the type: null for an object, zero for the numeric types, and false for a boolean. You don’t need to know the default value for char, but in case you are curious, it is ‘\u0000’ (NUL).

Inferring the Type with var

You have the option of using the keyword var instead of the type when declaring local vari-ables under certain conditions. To use this feature, you just type var instead of the primitive or reference type. Here’s an example:

public class Zoo {

public void whatTypeAmI() {

var name = “Hello”;

var size = 7;

}

}

The formal name of this feature is local variable type inference. Let’s take that apart. First comes local variable. This means just what it sounds like. You can only use this feature for local variables. The exam may try to trick you with code like this:

public class VarKeyword {

var tricky = “Hello”; // DOES NOT COMPILE

}

Wait a minute! We just learned the difference between instance and local variables. The variable tricky is an instance variable. Local variable type inference works with local vari-ables and not instance variables.