aspose file tools
The moose likes Beginning Java and the fly likes static members!! Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "static members!!" Watch "static members!!" New topic
Author

static members!!

Brian Smith
Ranch Hand

Joined: Oct 26, 2002
Posts: 232
hi folks,
i am wondering why static variable or method does not need an intance of a class where they are defined to access them? i.e, if a variable and method are static, we could like this:
classname.static valriable; WHY?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

Why? Because!
Static variables are shared among all the instances of a class, and therefore exist outside of any one instance. The classic example is something like

The static variable counts the number of Cows that are created. The method getTotalNumber() tells you how many Cows. You call it like

It would be silly to have to create another Cow just to find out how many Cows there are, right? It makes sense to ask how many there are even if you don't have a reference to a single one.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: static members!!
 
Similar Threads
static variables
Static variables in Static method
switch
static variable in static function
Static Variable Problem