Here's the difference: static members are class members while non-static members are object members. Notice that I say members here because the same applies to methods (functions) as well as attributes (variables). Now, check this:
1. All static members are automatically allocated in static memory (not heap or stack) at program initialization (I can't remember whether or not Java automatically initializes them as well).
2. Non-static members don't get allocated or initialized until you declare an object of that type. So, you can't call a non-static method or reference a non-static variable from a static method because the non-static members may not actually exist.
3. Whenever you create an objects of a given class type, each object of that type gets its own copy of any non-static members (methods as well as attributes), while they all share the exact same copies of any members (again, methods as well as attributes).
4. Since static data is initialized without the need to create any objects of that class type, any public static data can be accessed without creating an object of that type, i.e
ClassName.StaticMethodName().
Note, that the answer to your question lies mainly in #2.
Here is a simple example of static usage:
public class TestClass{ static count = 0; TestClass(){ count++; } public static getCount(){ return count; }}public class TestApp{ public static void main(String [] args){ System.out.println("# of test objects: " + TestClass.getCount()); //called statically without an object TestClass [] objects = new TestClass[10]; System.out.println("# of test objects: " + TestClass.getCount()); //called statically without an object System.out.println("# of test objects: " + objects[0].getCount()); //called by an object System.out.println("# of test objects: " + objects[5].getCount()); //called by an object }}Output:# of test objects: 0# of test objects: 10# of test objects: 10# of test objects: 10
Note that this is a way to achieve global access to functions in Java, but requires you to think a little more about what you're doing that just slapping a global on it an doing whatever.
peace and (trance) out
Mage
Edit: Added the object-call test cases.
[edited by - Mage2k on October 7, 2003 10:07:09 PM]
---------------------------------------------------There are 10 kinds of people in the world:Those that understand binary, and those that dont...Mage