Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

static method: a newbie question

0 views
Skip to first unread message

Usrnm

unread,
Oct 31, 2000, 12:25:00 PM10/31/00
to
Dear friends,

What does 'static' indicate when we place it before a method
name? When do we want to use it? Thanks.

Sent via Deja.com http://www.deja.com/
Before you buy.

Chad Podoski

unread,
Oct 31, 2000, 12:48:56 PM10/31/00
to
Static means that the method is not associated with a given instance of
the class it is in and therefore can be called without a class instance

Ex.

Class foo {

public static void test() {

}

public void notStatic() {

}
}

In order to call notStatic you would need to create a class instance of
foo

foo f = new foo();
f.notStatic();

You can call test without an instace of foo

test();

Note: if you manipulate class variable in a static method those class
variable must also be static

Usrnm

unread,
Oct 31, 2000, 3:52:21 PM10/31/00
to
Thank you, Chad, your explanation is so clear that I think I've
understood the problem now.

You have a wonderful Halloween!


In article <39FF0608...@cise.ufl.edu>,

Cat

unread,
Oct 31, 2000, 6:10:07 PM10/31/00
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Whoops! You forgot the class Chad.

foo.test();

Also the convention is to start all classes with a capital letter
e.g. Foo.test(). One of the other points about a static method (or
member/field) is that there is only one copy for the given class. So
if I had the following class

public class HalloweenKids
{
private static int lolyCount;

protected static void addLoly()
{
lolyCount++;
}
}

All the instances of HalloweenKids share their lollies so there is
only one copy of the total amount of lollies. In order to make it
clear that you are dealing with a static method you should use the
name of the class to call it and not an instance e.g.
HalloweenKids micky = new HalloweenKids();
HalloweenKids.addLoly();

not

micky.addLoly();

Cat

NEW THIS MONTH http://www.ratrobot.com/brainzilla/brainzilla.htm
What's the absolute limit of intelligence in this universe? And why
should I turn of the abflex2000 infomercials long enough to read out
it?
MY FREE APPLETS http://www.ratrobot.com/java/ratrobot_help.jar
CartoonPopUps. Put cartoon bubbles over your favourite image link
when the cursor is over them. And a simple menu applet that lets you
change the colors of the font,menu and background.
www.ratrobot.com Articles that challenge your ideas about yourself
and the world you live in.
"Chad Podoski" <cpod...@cise.ufl.edu> wrote in message
news:39FF0608...@cise.ufl.edu...

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 6.5.8 for non-commercial use <http://www.pgp.com>

iQA/AwUBOf7ESih0Y2LcENUAEQIPYwCgnUAndTZX2F1MePbyq1iIAFeY/KEAoJ4l
2teLSSNJ/2sH15xT8M8z71sE
=y9lX
-----END PGP SIGNATURE-----

0 new messages