Imsold4christ![]() Member Posts: 305 From: Gresham, OR, US Registered: 01-20-2001 |
Could someone explain to me the concept of the use of "static" in Java? I don't understand what it's purpose is at all. †Caleb† ------------------ |
||
GUMP![]() Member Posts: 1335 From: Melbourne, FL USA Registered: 11-09-2002 |
All instances of a static variable/object are global and thus any access to it references a single memory location (no copies are made when used in other sections of the program). Normally a class member must be accessed only in conjunction with an object of its class but with static it's possible to create a member that can be used by itself, without reference to a specific instance. The only negative is that methods declared static must only access static data and can only call other static methods. | ||
Onag Junior Member Posts: 5 From: Orange County, CA Registered: 01-25-2003 |
To clarify (using pseudo-code):
The output of the above pseudo-code would be:
While a normal variable is different for each object, a static variable applies to the class itself. All objects (instances) of the class have direct access to the same value. I hope this helps. -Nag [This message has been edited by Onag (edited March 16, 2004).] |
||
Imsold4christ![]() Member Posts: 305 From: Gresham, OR, US Registered: 01-20-2001 |
That does help, thank you both. I can see how static would be useful for variables in certain programs. However, I can't really think of a use for a static object. Could someone give an example of a static object too? †Caleb† ------------------ |
||
CobraA1![]() Member Posts: 926 From: MN Registered: 02-19-2001 |
Well, first things first: With the exception of the primitive datatypes, everything in Java is an Object ![]() Remember System.out.println? out is a static class, so you can refer to it without referring to a a specific instance of it. The main class is also that way - that way, there can only be one main class in your program, so the launcher knows where to start. ------------------ Switch Mayhem now available! Get it here |
||
Klumsy![]() Administrator Posts: 1061 From: Port Angeles, WA, USA Registered: 10-25-2001 |
quote: Does java really have such a restriction? i know that with static you can't access non-static data and methods without an instance, but if you have an instance of any object, surely static code, can access that instances data and methods just like any other code?
mylibimage.converttoJpg ... mylibimage.converttoBMP etc.. though its object orientated, there is is static data.. its basically a library wrapped up in an object but there are many instances of using static data.. i,e in singletons.. loginclass.dologin( name,password) and check if loginclass.isloggedin (a static bool) is true there are many instances when static sort of classes are great, like a screen object.. dealing with your ONE physical screen.. you wouldn't want to haveto create and dispose of the object whenever you wanted to use it.. Karl ------------------ |