aspose file tools*
The moose likes Beginning Java and the fly likes Whats differnce between class variable and instance variable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Whats differnce between class variable and instance variable?" Watch "Whats differnce between class variable and instance variable?" New topic
Author

Whats differnce between class variable and instance variable?

Karthik Ponnusamy
Greenhorn

Joined: Jan 05, 2010
Posts: 9
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

Joined: Oct 13, 2005
Posts: 39868
    
  28
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

Joined: Oct 13, 2005
Posts: 39868
    
  28
You can write
Karthik Ponnusamy
Greenhorn

Joined: Jan 05, 2010
Posts: 9
Thanks campbell.... I got it..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39868
    
  28
Well done
Amber Vaire
Greenhorn

Joined: Nov 04, 2012
Posts: 3
Thanks Campbell, most grateful.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

His name is Ritchie guys.


~ Mansukh
Amber Vaire
Greenhorn

Joined: Nov 04, 2012
Posts: 3
Lol ok thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Whats differnce between class variable and instance variable?