What are the local variables in Java


4.3 Variables



4.3.1 Basic properties

Variables are used to store data in the main memory of a program and, if necessary, to read or change it. There are three types of variables in Java:

  • Instance variablesthat are defined as part of a class definition and created together with the object.
  • Class variablesthat are also defined as part of a class definition, but exist independently of a specific object.
  • Local variablesthat are defined within a method or a block and only exist there.

In addition, the language definition also considers array components and the parameters of methods and exception handlers as variables.

A variable in Java is always typed. It is either of a primitive type or of a reference type. With the exception of a special case with array variables, which we will come back to later, all type checks are done at compile time. Java is therefore a type-safe language in the classic sense.

In order to assign a value to a variable of the type, and must be assignment compatible. Which types are assignment-compatible is explained at the end of this chapter in section 4.6.

Variables can be changed in two different ways:

Both options are explained in detail in Chapter 5.

4.3.2 Declaration of variables

A variable is declared in the form

A variable of the type with the name is created. In Java, variable declarations can be made anywhere in the program code. The following example program creates the variables,, and and displays their content on the screen:

001 / * Listing0402.java * / 002 003 publicclass Listing0402 004 {005 publicstaticvoid main (String [] args) 006 {007 int a; 008 a = 1; 009 char b = 'x'; 010 System.out.println (a); 011 double c = 3.1415; 012 System.out.println (b); 013 System.out.println (c); 014 boolean d = false; 015 System.out.println (d); 016} 017}Listing0402.java
Listing 4.2: Outputting simple variables

The output of the program is: 1 x 3.1415 false

As can be seen in this example, variables can be initialized when they are declared. To do this, simply append the desired value to the declaration after an assignment operator:

char b = 'x'; double c = 3.1415; boolean d = false;
Listing 4.3: Initializing Variables

4.3.3 Lifetime / Visibility

The visibility of local variables extends from the point of their declaration to the end of the method in which they were declared. If local variables have been created within a block, they are visible up to the end of the block. The lifetime of a local variable begins when the associated declaration statement is executed. It ends with the end of the method call. If local variables have been created within a block, their lifespan ends when the block is exited. In Java, it is not allowed to declare local variables that hide local variables of the same name from a block further out. On the other hand, it is permissible to hide class or instance variables.

Instance variables are created when a new instance of a class is created. They are visible within the entire class as long as they are not covered by local variables with the same name. In this case, however, access is possible with the help of the pointer: always accesses the instance or class variable, even if a local variable with the same name exists. When the associated object is destroyed, all instance variables are also destroyed.

Class variables live during the entire runtime of the program. The rules for their visibility correspond to those of instance variables.