3.1. Scope of variables

[1.1] Define the scope of variables

The scope of a variable specifies its life span and its visibility. In this section, we’ll cover the scopes of variables, including the domains in which they’re accessible. Here are the available scopes of variables:

  • Local variables (also known as method-local variables)
  • Method parameters (also known as method arguments)
  • Instance variables (also known as attributes, fields, and nonstatic variables)
  • Class variables (also known as static variables)

As a rule of a thumb, the scope of a variable ends when the brackets of the block of code it’s defined in get closed. This might be hard to understand now, but it will become clearer when you go through the examples. Let’s ...

Get OCA Java SE 8 Programmer I Certification Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.