The different types of variables in a Java program.

9
The different types of variables in a Java program

Transcript of The different types of variables in a Java program.

Page 1: The different types of variables in a Java program.

The different types of variables in a Java program

Page 2: The different types of variables in a Java program.

Different types of information

• Computer program needs information to operate correctly

The information used by computer programs are stored inside variables

• We can divide the information broadly into:

• As a result, computer programs use different kinds of variables specialized to store short term and long term information

• Short term information

• Long(er) term information

Page 3: The different types of variables in a Java program.

Different types of information (cont.)

• Furthermore, because variables can take up a lot of RAM memory, we organize the storage of the different kinds of variable in the RAM memory.

Page 4: The different types of variables in a Java program.

The different types of variables in the Java program

• Java has 4 different kinds of variables (See: http://download.oracle.com/javase/tutorial/java/nutsandbolts/variables.html)

• Class variables

• Instance variables

• Local variables

• Parameter variables    

Page 5: The different types of variables in a Java program.

The different types of variables in the Java program (cont.)

• General information on variables:

• Each type of variable has it's own characteristics (properties)

• The type of variable is determined by:

• Where the variable is defined

• Whether the keyword static was used in the variable definition.

Page 6: The different types of variables in a Java program.

Properties of variables

• Every variable has 2 properties:

• life time = the duration that a variable exists

• scope = the region in the program where the variable is accessible (can be used)

Page 7: The different types of variables in a Java program.

Properties of variables (cont.)

• Note:

• The different kinds of variables (class, instance, local and parameter variables) have different life time durations and different scopes

Page 8: The different types of variables in a Java program.

Why do programs have different kinds of variables

• The reason is to accommodate different needs:

• Short term information:

• Long term information:

Some information need only be retained within a (one) method (We call this kind of information local information)

• Some information must be retained across several methods

Page 9: The different types of variables in a Java program.

Why do programs have different kinds of variables (cont.)

• Local variables and parameter variables are used to store local (or short term) information

• Class variables and instance variables are used to store longer term information