Variables in Java
There are three variable's available in Java
Local Variable:
The Local Variable assigned inside of the method and outside of the Class. The scope of the Local Variables if with in the Method only.
Example:
Instance Variable:
Instance Variable available inside the Class and out side of the method. Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.
Example:
Static Variable:
The Static Variable also called as Class Variable. The Static/Class Variable declared inside the class and outside of the method.
Example:
The Interview Point of view:
What is difference between the Static/Class Variable and the Instance Variable ??
__________________________________________________________________________________
Data types in Java
There are three variable's available in Java
- Local Variable
- Instance Variable
- Static Variable
Local Variable:
The Local Variable assigned inside of the method and outside of the Class. The scope of the Local Variables if with in the Method only.
Example:
class Taxes {
void method_name()
{
int count; //Local Variable
/*...*/
}
}
Instance Variable:
Instance Variable available inside the Class and out side of the method. Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.
Example:
class Taxes { int count; //Instance Variable /*...*/ }
Static Variable:
The Static Variable also called as Class Variable. The Static/Class Variable declared inside the class and outside of the method.
Example:
class Taxes { static int count; //Static/Class Variable /*...*/ }
The Interview Point of view:
What is difference between the Static/Class Variable and the Instance Variable ??
__________________________________________________________________________________
Static/Class Variable
|
Instance Variable
|
Instance variables and class variables are
both member variables. They are both member
variables because they are both associated with a specific class.
|
|
Every object has it’s own copy of the
instance variables
|
Each object of the class does not have
its own copy of a class variable.
|
Now, it should be clear what the difference between instance and class variables is. Class variables only have one copy that is shared by all the different objects of a class, whereas every object has it’s own personal copy of an instance variable. So, instance variables across different objects can have different values whereas class variables across different objects can have only one value.
________________________________________________________________________________Data types in Java
|
` | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
No comments:
Post a Comment