aspose file tools*
The moose likes Beginning Java and the fly likes Difference between Static 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 "Difference between Static and Instance variable" Watch "Difference between Static and Instance variable" New topic
Author

Difference between Static and Instance variable

NitishK Kumar
Ranch Hand

Joined: Oct 20, 2010
Posts: 40

What is the difference between static and instance variable?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30749
    
156

A static variable is shared by all instances of the class.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jacob Coddaire
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

Instance versus static methods
Each Java method is either an instance method or a static method. Any method not marked with the static keyword is an instance method. Lastly, all Java methods must be defined within a class. In order to call an instance method, you must first have an instance of the class within which the method you want to call is defined. Suppose we want to call an instance method named "method1" defined in class "ClassA". We could do the following:

ClassA x = new ClassA();
x.method1();

If method1 were instead static, then we would call the method by referring directly to the class itself:

ClassA.method1();

For more on the instance/static distinction, search Google for "java instance versus static method"
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
Static Variable= Defined at Class Level
Instance Variable =Defined at Instace/Object Level.

Static Variables are initialized ,loaded with Class itself.

But instance variable initialized when Object for that Class is instantiated.

In other words - For 1 class ,its different objects can have different values for same instance variable.(Each instance =can have its own value)
But for same class ,static variable (since belongs to class itself ) -same value is shared by each object.





NitishK Kumar
Ranch Hand

Joined: Oct 20, 2010
Posts: 40

Lucky J Verma wrote:Static Variable= Defined at Class Level
Instance Variable =Defined at Instace/Object Level.

Static Variables are initialized ,loaded with Class itself.

But instance variable initialized when Object for that Class is instantiated.

In other words - For 1 class ,its different objects can have different values for same instance variable.(Each instance =can have its own value)
But for same class ,static variable (since belongs to class itself ) -same value is shared by each object.



Thank you Lucky Sir, Actually this was my doubt which I wanted to clear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between Static and Instance variable