• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Whats differnce between class variable and instance variable?

 
Karthik Ponnusamy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am having a below code, but need to understand the difference between class variable and instance variable?

more over, how to call the variable "i" in the below class? [static & non-static reference error]

 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must have forgotten which forum you were on. I am sure Henry won’t mind if I move this discussion. You also appear to have forgotten the code button. I shall go back to your code and use the code button, and you will see how much better it looks.

Class variables is another name for static fields. When you are in a static method, you are working at the level of the class, not at the level of an instance, so you cannot access any instance members of the class. Nor can you use the keywords this and super.
The reason is that when you are in a static method, there might be no instances of the class. So there are no instance fields you can access. Or there might be several instances; you cannot tell which instance to use. So static == no access to instance members.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write
 
Karthik Ponnusamy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks campbell.... I got it..
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
 
Amber Vaire
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell, most grateful.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
His name is Ritchie guys.
 
Amber Vaire
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lol ok thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic