You are on page 1of 4

In Java, all variables must be declared before they can be used.

The basic form of a variable declaration is shown here:

type identifier [ = value][, identifier [= value] ...] ;

The type is one of Java's datatypes. The identifier is the name of the variable. To declare more than one variable of the specified type, use a comma-separated list. Here are several examples of variable declarations of various types. initiali!ation.
int a, b, c; // declares three ints, a, b, and c. int d = 3, e, f = 5; // declares three more ints, initializing // d and f. byte z = ; // initializes z. double pi = 3.!"!5#; // declares an appro$imation of pi. char $ = %$%; // the variable $ has the value %$%.

ote that some include an

This chapter will explain various variable types available in Java "an#ua#e. There are three $inds of variables in Java:

"ocal variables Instance variables %lass&static variables

"ocal variables:

"ocal variables are declared in methods, constructors, or bloc$s. "ocal variables are created when the method, constructor or bloc$ is entered and the variable will be destroyed once it exits the method, constructor or bloc$. 'ccess modifiers cannot be used for local variables. "ocal variables are visible only within the declared method, constructor or bloc$. "ocal variables are implemented at stac$ level internally. There is no default value for local variables so local variables should be declared and an initial value should be assi#ned before the first use.

Here, age is a local variable. This is defined inside pupAge() method and its scope is limited to this method only.
public class &est' public void pup(ge)*' int age = +; age = age , -; .ystem.out.println)/0uppy age is 1 / , age*; 2 public static void main).tring args[]*' &est test = ne3 &est)*;

test.pup(ge)*; 2 2

This would produce the followin# result:

0uppy age is1 -

)ollowin# example uses age without initiali!in# it, so it would #ive an error at the time of compilation.
public class &est' public void pup(ge)*' int age; age = age , -; .ystem.out.println)/0uppy age is 1 / , age*; 2 public static void main).tring args[]*' &est test = ne3 &est)*; test.pup(ge)*; 2 2

This would produce the followin# error while compilin# it:

&est.4ava1"1variable number might not have been initialized age = age , -; 5 ! error

Instance variables:

Instance variables are declared in a class, but outside a method, constructor or any bloc$. *hen a space is allocated for an ob+ect in the heap, a slot for each instance variable value is created. Instance variables are created when an ob+ect is created with the use of the $eyword 'new' and destroyed when the ob+ect is destroyed. Instance variables hold values that must be referenced by more than one method, constructor or bloc$, or essential parts of an ob+ect's state that must be present throu#hout the class. Instance variables can be declared in class level before or after use. 'ccess modifiers can be #iven for instance variables. The instance variables are visible for all methods, constructors and bloc$ in the class. ormally, it is recommended to ma$e these variables private ,access level-. However visibility for subclasses can be #iven for these variables with the use of access modifiers. Instance variables have default values. )or numbers the default value is ., for /ooleans it is false and for ob+ect references it is null. 0alues can be assi#ned durin# the declaration or within the constructor.

Instance variables can be accessed directly by callin# the variable name inside the class. However within static methods and different class , when instance variables are #iven accessibility- should be called usin# the fully 1ualified name . ObjectReference.VariableName.

import; public class 7mployee' // this instance variable is visible for any child class. public .tring name; // salary variable is visible in 7mployee class only. private double salary; // &he name variable is assigned in the constructor. public 7mployee ).tring emp8ame*' name = emp8ame; 2 // &he salary variable is assigned a value. public void set.alary)double*' salary =; 2 // &his method prints the employee details. public void print7mp)*' .ystem.out.println)/name 1 / , name *; .ystem.out.println)/salary 1/ , salary*; 2 public static void main).tring args[]*' 7mployee emp9ne = ne3 7mployee)/:ansi;a/*; emp9ne.set.alary)!+++*; emp9ne.print7mp)*; 2 2

This would produce the followin# result:

name 1 :ansi;a salary 1!+++.+

%lass&static variables:

%lass variables also $nown as static variables are declared with the static $eyword in a class, but outside a method, constructor or a bloc$. There would only be one copy of each class variable per class, re#ardless of how many ob+ects are created from it. 2tatic variables are rarely used other than bein# declared as constants. %onstants are variables that are declared as public&private, final and static. %onstant variables never chan#e from their initial value. 2tatic variables are stored in static memory. It is rare to use static variables other than declared final and used as either public or private constants. 2tatic variables are created when the pro#ram starts and destroyed when the pro#ram stops. 0isibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class. 3efault values are same as instance variables. )or numbers, the default value is .4 for /ooleans, it is false4 and for ob+ect references, it is null. 0alues can be assi#ned durin# the

declaration or within the constructor. 'dditionally values can be assi#ned in special static initiali!er bloc$s.

2tatic variables can name . ClassName.VariableName.








*hen declarin# class variables as public static final, then variables names ,constants- are all in upper case. If the static variables are not public and final the namin# syntax is the same as instance and local variables.

import; public class 7mployee' // salary variable is a private static variable private static double salary; // <70(:&=78& is a constant public static final .tring <70(:&=78& = /<evelopment /; public static void main).tring args[]*' salary = !+++; .ystem.out.println)<70(:&=78&,/average salary1/,salary*; 2 2

This would produce the followin# result:

<evelopment average salary1!+++

Note: If the variables are access from an outside class the constant should be accessed as (mployee.3(5'6T7( T

*hat is ext8
9ou already have used access modifiers , public : private - in this chapter. The next chapter will explain you 'ccess 7odifiers and on 'ccess 7odifiers in detail.
5revious 5a#e 5rint 0ersion 53) 0ersion ext 5a#e

Share on facebook