• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java (beginner)

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is a class variable and Instance variable ?
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sudhir,
Look, every variable is different from other on the basis of its scope and access. Instance and class variables are the same. See,
class XYZ{
// Variables declared here (after class braces and not in any // method braces) are called instance or class variables.
myMethod(){
// Variables declared here ( in the braces of a //method) are known // as local( to the method //where they are ) variables .
}
}
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a little more to it than that Fawad. Class level variables are static, meaning that if there are 20 instances of a class, they all share that variable. So if object A changes a class variable to 20, then it will be 20 when you reference it from object B as well.
Instance variables belong to an instance of a class. Every object holds it own value. So if object A changes its instance variable to 20, object B will not have its instance variable changed also.
The third type is local variables, and those are variables that are inside of methods.
Bill
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by PadmajaSudhir:
what is a class variable and Instance variable ?


Perhaps you take a look at http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic