Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when to use static?

 
Moses Henda
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i wish to enquire when do we need declare the properties of a class
static.And whats the difference between normal variables and static variables?
 
Maureen Charlton
Ranch Hand
Posts: 218
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally each object has its own copy of the members defined within the class that was used to create it; these are known as instance variables. The static modifier can be used to state that a particular member is to be shared between all the objects for that class. Static members are sometimes referred to as class variables because the variable is related to the class, not to each object created from the class.

The static specifier ensures that the allocation only occurs once, regardless of the number of objects that are created. This provides the programmer with a common variable for all instances of a class.

The static specifier cannot be used with local variables, i.e. variables declared within a method. But it can be applied to methods and has the same effect, for example, the main method of a class needs to be static so the JVM can run the program without creating an instance of the class.

Hope the above helps (I also have had some fun understanding this)
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static is a class level variable, shared by all instances of that class.
This should be a good starting point - http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html
 
Moses Henda
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thx guys
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic