Java variables must be declared before they are used. The basic form of variable declaration is as shown:
type identifier [= value][, identifier [ = values] ... ];
The type can be called as 'datatypes', identifier is a 'variable' and variable must be initialized by specifying an equal sign and a value. Comma is used if more than one variable is declared, to separate one variable from another.
Basically there are three types of variables in Java:
Class Variable:
Here are the list of default values of particular datatypes.
type identifier [= value][, identifier [ = values] ... ];
The type can be called as 'datatypes', identifier is a 'variable' and variable must be initialized by specifying an equal sign and a value. Comma is used if more than one variable is declared, to separate one variable from another.
Basically there are three types of variables in Java:
- Local variable.
- Class variable.
- Instance variable.
Local Variables :
- Local variable are local to method.
- This is visible only to method in which they are declared.
- Local variable does not use any default value like class variable so this should be declared before they are used.
- Access modifier cannot be used for local variable.
Example:
public class LocalVariable
{
public static void main(String args[])
{
System.out.println("a");
}
void method()
{
a=10;
}
}
This will throw an error when executing. As the Variable "a" is declared and initialized inside 'method', which becomes method variable and we are trying to access the same in main method where it does not have any scope.
Example:
public class LocalVariable1This will give you the output: 10
{
public static void main(String args[])
{
int a = 10; // Declaration and initialization of variable 'a'
System.out.println(a);
}
}
Class Variable:
- Class variable is declared in a class but outside the method, right in the beginning or right at the end.
- The scope of the class variable is for all the method in that particular class.
- Unlike Local Variable, Class Variable need not be initialized as they take default values.
Here are the list of default values of particular datatypes.
| Data type | Default values |
|---|---|
| boolean | FALSE |
| char | \u0000(That is nothing) |
| byte | 0 |
| short | 0 |
| int | 0 |
| long | 0L |
| float | 0.0f |
| double | 0.0d |
| string | null |
Example:
public class ClassVariableThe output will be: 0
{
int a;
public static void main(String args[])
{
ClassVariable x = new ClassVariable();
x.method();
}
void method()
{
System.out.println(a);
}
}
Here it will print the default value of datatype.
Instance Variable:
- Instance variable are created when objects are instantiated and therefore they are associated with objects.
- They take different values for each object.
- Static variable have only one memory location and should be shared by all instances.
- Non-static variable take separate value for different instances.
Example:
public class InstanceVariable
{
static int a=10;
int b=20;
public static void main(String args[])
{
InstanceVariable v1 = new InstanceVariable();
v1.a = 40;
v1.b = 50;
InstanceVariable v2 = new InstanceVariable();
v2.a = 60;
v2.b = 70;
InstanceVariable v3 = new InstanceVariable();
System.out.println(v3.a);
System.out.println(v3.b);
System.out.println(v3.a);
}
}
The output will be:
60 20 60
In this program there is static variable and non static variable(non static is 'int b')
Static memory has one memory location so it accepts the last value as the other value is re-written by the last one. Non Static variable can take different value for different instances, therefore it continues giving result with another values.




0 comments:
Post a Comment