• Dear Guest, Please do NOT forget to join our JAVA JOBS (ONLY) FACEBOOK GROUP to be aware of latest JAVA openings, and you may also post any JAVA related queries/jobs.

What are the types of variables in Java ?

Any one please tell me different types of variables in Java ? I mean what is class varible, instance varible, etc in Java ?
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. :)

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...!
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
(a)float (b)double

All the best
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 .
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