var in the Real World – Building Blocks – 1Z0-829 Study Guide

var in the Real World

The var keyword is great for exam authors because it makes it easier to write tricky code.

When you work on a real project, you want the code to be easy to read.

Once you start having code that looks like the following, it is time to consider using var:

PileOfPapersToFileInFilingCabinet pileOfPapersToFile = new PileOfPapersToFileInFilingCabinet();

You can see how shortening this would be an improvement without losing any information:

var pileOfPapersToFile = new PileOfPapersToFileInFilingCabinet();

If you are ever unsure whether it is appropriate to use var, we recommend “Local Variable Type Inference: Style Guidelines,” which is available at the following location.

https://openjdk.java.net/projects/amber/LVTIstyle.html

Managing Variable Scope

You’ve learned that local variables are declared within a code block. How many variables do you see that are scoped to this method?

public void eat(int piecesOfCheese) {

int bitesOfCheese = 1;

}

There are two variables with local scope. The bitesOfCheese variable is declared inside the method. The piecesOfCheese variable is a method parameter. Neither variable can be used outside of where it is defined.

Limiting Scope

Local variables can never have a scope larger than the method they are defined in. However, they can have a smaller scope. Consider this example:

  • public void eatIfHungry(boolean hungry) {
  • if (hungry) {
  • int bitesOfCheese = 1;
  • }  // bitesOfCheese goes out of scope here
  • System.out.println(bitesOfCheese);  // DOES NOT COMPILE
  • }

The variable hungry has a scope of the entire method, while the variable bitesOfCheese has a smaller scope. It is only available for use in the if statement because it is declared inside of it. When you see a set of braces ({}) in the code, it means you have entered a new block of code. Each block of code has its own scope. When there are multiple blocks, you match them from the inside out. In our case, the if statement block begins at line 4 and ends at line 6. The method’s block begins at line 3 and ends at line 8.

Since bitesOfCheese is declared in an if statement block, the scope is limited to that block. When the compiler gets to line 7, it complains that it doesn’t know anything about this bitesOfCheese thing and gives an error.

Remember that blocks can contain other blocks. These smaller contained blocks can ref-erence variables defined in the larger scoped blocks, but not vice versa. Here’s an example:

  1. public void eatIfHungry(boolean hungry) {
  1. if (hungry) {
  1. int bitesOfCheese = 1;
  1. {
  • var teenyBit = true;
  • System.out.println(bitesOfCheese);
  • }
  • }
  • System.out.println(teenyBit);  // DOES NOT COMPILE
  • }

The variable defined on line 18 is in scope until the block ends on line 23. Using it in the smaller block from lines 19 to 22 is fine. The variable defined on line 20 goes out of scope on line 22. Using it on line 24 is not allowed.