Help Wanted

static – imsold4christ

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†

------------------
"True friendship is not characterized by the absence of conflict, but by the ability to resolve conflict."

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):


class User
{
int logins = 0
static int totalLogins = 0

Login(page)
{
logins++
totalLogins++
}
}

User tom
User alice

tom.Login(welcome)
tom.Login(guestbook)
tom.Logout()

alice.Login(welcome)
alice.Login(links)
alice.Login(controlpanel)
alice.Logout()

print tom.logins
print alice.logins
print User.totalLogins

The output of the above pseudo-code would be:


2
3
5

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†

------------------
"True friendship is not characterized by the absence of conflict, but by the ability to resolve conflict."

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.

------------------
There are two kinds of people: those who say to God, "Thy will be done," and those to whom God says, "All right, then, have it your way." -- C. S. Lewis

Switch Mayhem now available! Get it here
Codename: Roler - Writing object code and GUI.

Klumsy

Administrator

Posts: 1061
From: Port Angeles, WA, USA
Registered: 10-25-2001
quote:
The only negative is that methods declared static must only access static data and can only call other static methods.


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?


caleb:
often static objects are just used a a library of functions.. i.e lets say you have a library to convert various images

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..
basically if there only needs to be one isntance of the data then its a good way to use it. an example i often use is a login object
only one user can every be logged in at a time, so we don't need muliple instances.. so the object contains methods to login , logout etc, but it also contains data like who is logged in and a list of their permissions etc - all declare as static.. and to use this object i don't need to create an instance i can just do

loginclass.dologin( name,password)

and check if loginclass.isloggedin (a static bool) is true
or or get loginclass.username etc etc

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

------------------
Karl /GODCENTRIC
Visionary Media
the creative submitted to the divine.
Husband of my amazing wife Aleshia
Klumsy@xtra.co.nz