What are the types of variables in Java ?

#1
Any one please tell me different types of variables in Java ? I mean what is class varible, instance varible, etc in Java ?
 
#2
class varibale : a data member which is declared with static modifier.
Instance variable : a data member in a class with out static modifier.

The main difference between the class variable and Instance variable is
,
first time, when class is loaded in to memory, then only memory is allocated for all class variables.
That means, class variables not depends on the Objets of that classes. what ever number of objects are there, only one copy is created at the time of class loding.
The main advantage of this class variable is, suppose if we want to maintain a counter to count the no of object of type that class, in that case we use variable counter as the static variable. :)
 
#4
Hi,

public class Boat
{

// Class variables
private static float boatFee;

// Instance variables

private String registration;
private String flag;
private String captainName;
private float lenght;
private float tonnage;
}


I think now you will get an idea.

Revert back if you want any clarification.

All the best...!
 
#10
Hi,
In java there are 8 varibles types means (datatypes ) under categirised in to 4 types.(1) int
(a)byte ( b)short (c)int (d)long
(2)float
(a)float (b)double
(3)char
(4)boolean

All the best
 
#11
Thanks 4 ur explanation . It will help me as well as other persons ?
But my Q is can we tell the interviewer there r three types of variables
1. Instance variable
2. class variable
3. local variable
for his Q What are the types of variables in java ?.
thanks to participate in this forem .
 
#17
Different types of variables depending on their scope.

Following are the kinds of variables categorized according to their scope:

  • Static variables
  • Instance variables
  • Method local and Method parameters
  • Block variables
 
Top