1.4.13 Local Variable Initialization

A Local Variable Can Be Given Its First Value When It Is Declared

A variable declared inside a method is a local variable.

A common pattern is to declare and initialize it in one statement:

int scoreDifference = 2;

This statement does two things:

  1. declares a local variable named scoreDifference ;
  2. gives it the initial value 2 .
Initialization Establishes the Variable's Starting Value

Consider:

double distance = Math.Abs(playerX - ballX);

The local variable is:

distance

Its type is:

double

Its initial value comes from:

Math.Abs(playerX - ballX)

The right side is evaluated first.

The result becomes the local variable's initial value.

A Calculation Can Provide the Initial Value

For example:

int totalScore = homeScore + awayScore;

The initialization flow is:

Activity diagram showing homeScore awayScore, then calculated value, then totalScore.
HomeScoreAwayScore.
  • New
  • Changed
  • Removed

The variable can then be reused later in the same scope.

A Method Call Can Provide the Initial Value

For example:

double roundedDistance = Math.Round(distance);

The Math.Round method produces a numeric result.

That returned value becomes the initial value of:

roundedDistance

You do not need to redesign the Math method.

You simply use the value it provides.

Initialization Is Different from Later Assignment

Initial declaration:

int scoreDifference = 2;

Later assignment:

scoreDifference = 4;

The first statement creates the local variable and establishes its first value.

The second changes the value of an already-declared variable.

The Type Still Matters

For:

int scoreDifference = 2;

the initial value must be compatible with int.

For:

bool shouldDisplay = true;

the initial value must be compatible with bool.

For:

string message = "Goal scored";

the initial value must be compatible with string.

Initialize from a Parameter When Useful

Suppose:

public void DisplayAdjustedScore(int score)
{
    int adjustedScore = score;
}

The local variable begins with the current parameter value.

The parameter and local variable remain separate names.

They simply contain equal values at that moment.

Initialize from an Object Field

For example:

int currentNumber = player1.jerseyNumber;

The local variable receives the current field value.

Changing currentNumber later does not automatically change player1.jerseyNumber.

They are separate storage locations.

Do Not Create a Local Variable Without a Purpose

This:

int temporaryValue = player1.jerseyNumber;

is useful only if the method needs a temporary named value for later work.

Do not create extra variables merely to make a simple expression longer.

Use a local variable when its name or reuse makes the method clearer.

Scope Begins Where the Local Variable Is Declared

A local variable can be used only within its valid scope.

If it is declared inside a method, another unrelated method does not automatically know that local variable.

This is different from a field, which belongs to object state.

The Main Pattern
Type variableName = initialValue;

Read it as:

That starting value may come from:

  • a literal;
  • a parameter;
  • a field;
  • a calculation;
  • a method result.