Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.3. Variables and blocks

3.3. Variables and blocks
Last updated: 23 January 2013.


3.3.1. Variables

In the Java language, a variable must be declared before it can be used. It can be declared as a class field (then its scope is the entire class), in a method (its scope is the method) or in a block contained in a method (its scope is the block). Blocks will be explained in the next section.

A variable must have a type: either an object type or a primitive type. According to the Java code conventions, a variable name must start with a small letter. As for class names and identifiers in general, you should avoid using hyphens and underscores as separators in the variable names. Instead, you should use a mix of capital letters and small letters. For example, myVariableName is better than my-variable-name. Variable names are case sensitive. Consequently, myVariableName is different from myvariablename

The basic format of a variable declaration is: <type> <identifier> [ = <initialValue> ];

The declaration ends with a semicolon. The initial value is optional and the identifier is the variable's name. For example, the following statement declares an integer named x:

int x;

You can declare several variables of the same type in a single declaration:

int x, y, z;

If you want the variable x to have an initial value of 10, here is the declaration:

int x = 10;

Here are several variables declarations of various primitive types:

int x = 10;
char c = 'i';
boolean bool = false;
float f = 0.5F;
double d = 15.7D;
long id = 1523L;

In the examples above, long id = 1523L; is the same as long id = 1523;. Likewise, you can omit the letter F and the letter D appearing in the float and double variables declarations.

If a primitive type variable is declared without being initialized, it is assigned a default value by the compiler. If an object variable is declared without being initialized, then it is assigned a null value. Here is the list of the default values for the primitive types:

Type Default value
boolean false
char '\u0000'
byte 0
short 0
int 0
long 0L
float 0.0F
double 0.0D



3.3.2. Blocks

A block is a series of statements enclosed by braces. When a variable is declared in a block, it is only visible in that block. Let's have a look at the class BlockExample shown below:

public class BlockExample {

   
int fieldVariable = 0;


   
public void method1(){

         
boolean bool = true;

         
// inside the block of method1...

          // The variable fieldVariable is visible here
   
}


   
public void method2(){

         
double weight = 103.5;

         
// The variable fieldVariable is visible here

         
{
               
// Block declared inside method2

               
int count = 10;

               
// The variable fieldVariable is still visible here

                // The variable weight is visible here
         
}

         
// The variable count is not visible here
   
}

}


Keywords explanation:  The keyword public preceding the keyword class means that this class can be accessed from any other outside class. More generally, when a class, a field or a method is declared public, then it can be accessed from any other outside class. The methods method1 and method2 can be accessed from an outside class since they are declared public and the keyword void declares that they do not return any value.


Variables discussion:  The variable fieldVariable is a global variable since it is declared outside any method. Hence, its scope is the entire class, which means it is visible and can be accessed in method1 and method2. The variable bool is local to method1 and thus cannot be accessed in method2. Finally, the variable weight is visible everywhere within method2 but the variable count is visible only within its block.


The next tutorial discusses the constants and enum types of the Java language.


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.3. Variables and blocks
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.4. Constants and enum types

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7