what is difference between Class variables and Instance variables?

Discussion in 'Java' started by rahul8143@gmail.com, Aug 14, 2005.

  1. Guest

    hello,
    what is difference between Class variables and Instance variables?
    , Aug 14, 2005
    #1
    1. Advertising

  2. fartymcfly Guest

    On Sun, 14 Aug 2005 03:13:19 -0700, rahul8143 wrote:

    > hello,
    > what is difference between Class variables and Instance variables?


    A class variable is visible/usable to all methods within that class. A
    instance variable is only visible/usable within the method (instance) that
    it has been declared in. Its all about variable scope my friend!

    regards,

    fartymcfly
    fartymcfly, Aug 14, 2005
    #2
    1. Advertising

  3. Guest

    , Aug 14, 2005
    #3
  4. Benoy Guest

    Class Variable in Java

    In Java Variables are of many types
    out of that these are two

    Class Variable

    As you told about class variables they can be used any where in that
    class
    but, as java is an object oriented so the variables can be used in
    other classes by creating objects

    thank you
    Benoy
    Benoy, Aug 14, 2005
    #4
  5. <> wrote in message
    news:...
    > hello,
    > what is difference between Class variables and Instance variables?
    >


    Instance variables belong to the object instance. They're the fields of the
    object. Class variables belong to the class as a whole--they're static
    variables. I recommend Roedy's definitions here:

    http://mindprod.com/jgloss/instancevariable.html or if you're up to it the
    JLS definition here
    http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#28536

    Cheers,
    Matt Humphrey http://www.iviz.com/
    Matt Humphrey, Aug 14, 2005
    #5
  6. Roedy Green Guest

    Roedy Green, Aug 14, 2005
    #6
  7. Roedy Green Guest

    On Sun, 14 Aug 2005 10:50:59 GMT, fartymcfly
    <> wrote or quoted :

    >A class variable is visible/usable to all methods within that class. A
    >instance variable is only visible/usable within the method (instance) that
    >it has been declared in. Its all about variable scope my friend!


    No. You have instance variables confused with local variables.
    Roedy Green, Aug 14, 2005
    #7
  8. fartymcfly Guest

    On Sun, 14 Aug 2005 21:04:45 +0000, Roedy Green wrote:

    > On Sun, 14 Aug 2005 10:50:59 GMT, fartymcfly
    > <> wrote or quoted :
    >
    >>A class variable is visible/usable to all methods within that class. A
    >>instance variable is only visible/usable within the method (instance) that
    >>it has been declared in. Its all about variable scope my friend!

    >
    > No. You have instance variables confused with local variables.


    I stand corrected!

    fartymcfly
    fartymcfly, Aug 15, 2005
    #8
  9. Virgil Green Guest

    wrote:
    > hello,
    > what is difference between Class variables and Instance variables?


    Class variables are more broad-minded than their narrow-minded cousins,
    Instance variables.

    --
    Virgil
    Virgil Green, Aug 15, 2005
    #9
  10. msankar81

    Joined:
    Feb 18, 2008
    Messages:
    1
    Static variable, the field is allocated when the class is created. It belongs to the class and not any object of the class. It is class variable.

    Instace variable, the field is allocated when the class is instanciated to the class is called instance variable or non-static variable

    Static variable has only one copy for all the methods in class while instance variable has many copy.

    Class can access only static variable while object can access both class and instance variable.
    msankar81, Feb 18, 2008
    #10
  11. jporter892

    Joined:
    Jun 6, 2011
    Messages:
    1
    They are both member variables, but there are also differences read here for great, detailed information regarding the difference between class and member variables:

    Class vs instance variable/
    jporter892, Jun 6, 2011
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,029
  2. Sridhar R
    Replies:
    14
    Views:
    1,381
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  3. John M. Gabriele
    Replies:
    18
    Views:
    1,142
    Steven Bethard
    Feb 18, 2005
  4. Chad
    Replies:
    11
    Views:
    496
    Abu Yahya
    Jun 11, 2011
  5. Replies:
    3
    Views:
    106
Loading...

Share This Page