What are the types of variables in Java ?

Discussion in 'Object Oriented Concepts' started by nishad, Nov 3, 2009.

  1. nishad

    nishad New Member

    Any one please tell me different types of variables in Java ? I mean what is class varible, instance varible, etc in Java ?
  2. azar.mhd

    azar.mhd New Member

    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. :)
  3. nishad

    nishad New Member

    Thank u can u explain with an example with java program
  4. azar.mhd

    azar.mhd New Member


    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...!
  5. nishad

    nishad New Member

    Hi Azar Assalamu alaikum and Thank u so much.
  6. azar.mhd

    azar.mhd New Member

    Waallaikum Assalam.

    Mention not..

  7. khadangap

    khadangap New Member

    there also a local variable or method variable
  8. azar.mhd

    azar.mhd New Member


    the variables declared inside the method can be called as local variables.
  9. khadangap

    khadangap New Member

    Hi friend ,

    So can't we tell as a another type of variable ?
  10. vijaybhaskar

    vijaybhaskar New Member

    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
  11. khadangap

    khadangap New Member

    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 .
  12. khadangap

    khadangap New Member

    Hi Vijay,
    Thanks $ ur valuable information .I remembered it .
    All the best .
  13. azar.mhd

    azar.mhd New Member


    there are three types of variables,
    Class variables, Instant variable and local variables.
  14. anoop.chiliveri

    anoop.chiliveri New Member

    Types of variables according to my knowledge are

    Local variables
    Instance variables
    Static variables
  15. Static variables of a class called class level variables,where as not static variables are called as instance variables
  16. chsrinu

    chsrinu New Member

    Interger, Float ,String
  17. anneroberts

    anneroberts Member

    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

Share This Page