Dear Folks !
In this blog I will be discussing different types of variables in Java. Primarily following three types of variables can be defined in Java,
1. Instance Variables
2. Class Variables
3. Local variables
Let's discuss each one in detail. Instance variables are used to represent specific state of an object and normally they are attributes of a class. For each instance separate memory is allocated for them on heap. They are always accessed with an object/instance. As the state for this variable varies object to object, it's also called non-static variable.
An example to demonstrate the use of instance variables.
class Account
{
int account_number; // An instance variable
double balance; // An instance variable
-----
----- }
class Bank
{
public static void main(String[] args)
{
Account a1 = new Account( );
a1.account_number = 123; // like this an object is used to access instance variable
a1.balance = 5000;
System.out.println("Account Number :"+a1.account_number);
System.out.println("Balance:"+a1.balance);
}
}
Now if you want minimum balance as one of the attribute of an Account, the you can define one more variable as min_bal, but what should be the type of variable. If you declare it as instance variable, the it's state/value is stored with every object(waste of memory), as the value will remain common for all, we can store it in single place and access it for all objects of that class. This is done by define it with a qualifier static. As it's state is for entire class, these are called class variables and hence are accessed with class name.
An example to demonstrate the use of class variables.
class Account
{
int account_number; // instance variable
double balance; // instance variable
static double min_bal; // static variable/class variable
-----
----- }
class Bank
{
public static void main(String[] args)
{
Account a1 = new Account( );
a1.account_number = 123;
a1.balance = 5000;
Account.min_bal=1000; // like this class name is used to access class variable
System.out.println("Account Number :"+a1.account_number);
System.out.println("Balance:"+a1.balance);
System.out.println("Minimum Balance:"+Account.min_bal);
}
}
When you declare any variable in a method, it's scope will become local and such variables are called local variable. In the above example ' a1 ' is a local variable. The memory is from stack, hence they are aslo called stack variables.
The default initial values for these variable are as below
class DemoLocal
{
public static void main(String args[])
{
int i;
System.out.println(++i);
}
}
If you compile this program. it will flag a compile time error like variable 'i' not initialized.
For any queries please write in comment section.
In this blog I will be discussing different types of variables in Java. Primarily following three types of variables can be defined in Java,
1. Instance Variables
2. Class Variables
3. Local variables
Let's discuss each one in detail. Instance variables are used to represent specific state of an object and normally they are attributes of a class. For each instance separate memory is allocated for them on heap. They are always accessed with an object/instance. As the state for this variable varies object to object, it's also called non-static variable.
An example to demonstrate the use of instance variables.
class Account
{
int account_number; // An instance variable
double balance; // An instance variable
-----
----- }
class Bank
{
public static void main(String[] args)
{
Account a1 = new Account( );
a1.account_number = 123; // like this an object is used to access instance variable
a1.balance = 5000;
System.out.println("Account Number :"+a1.account_number);
System.out.println("Balance:"+a1.balance);
}
}
Now if you want minimum balance as one of the attribute of an Account, the you can define one more variable as min_bal, but what should be the type of variable. If you declare it as instance variable, the it's state/value is stored with every object(waste of memory), as the value will remain common for all, we can store it in single place and access it for all objects of that class. This is done by define it with a qualifier static. As it's state is for entire class, these are called class variables and hence are accessed with class name.
An example to demonstrate the use of class variables.
class Account
{
int account_number; // instance variable
double balance; // instance variable
static double min_bal; // static variable/class variable
-----
----- }
class Bank
{
public static void main(String[] args)
{
Account a1 = new Account( );
a1.account_number = 123;
a1.balance = 5000;
Account.min_bal=1000; // like this class name is used to access class variable
System.out.println("Account Number :"+a1.account_number);
System.out.println("Balance:"+a1.balance);
System.out.println("Minimum Balance:"+Account.min_bal);
}
}
When you declare any variable in a method, it's scope will become local and such variables are called local variable. In the above example ' a1 ' is a local variable. The memory is from stack, hence they are aslo called stack variables.
The default initial values for these variable are as below
Variable type
|
Default value for primitive type
|
Default value for reference type
|
Instance
|
Zero
|
null
|
Class variable
|
Zero
|
null
|
Local variable
|
Not initialized to any default value
|
null
|
class DemoLocal
{
public static void main(String args[])
{
int i;
System.out.println(++i);
}
}
If you compile this program. it will flag a compile time error like variable 'i' not initialized.
For any queries please write in comment section.