methods

i "stole" this code below from a website and i was wondering why the method ShowBools() is not static. What does it mean if a static method is a class method? can someone please explain?

class ABC

{

// Initialize the boolean with the default value of false.

boolean first;

// initialize the boolean with the value explicitly set to false.

boolean second =false;

// initialize the boolean with the value explicitly set to true.

boolean third =true;

void ShowBools()

{

//Print out the value of each of the three booleans

System.out.println("The value of first is " + first);

System.out.println("The value of second is " + second);

System.out.println("The value of third is " + third);

}

publicstaticvoid main(String args[])

{

//Create an instance of the class ABC

ABC abc =new ABC();

//Change the value of third to false.

abc.third =false;

//Show the three booleans on the screen with their respective values.

abc.ShowBools();

}

}

[1961 byte] By [mark_8206a] at [2007-11-26 19:18:02]
# 1

Have a look at this example:ABC abc= new ABC();

ABC def= new ABC();

...

abc.first= true;

def.second= true;

abc.ShowBools();

def.ShowBools();

The ShowBools() method shows the three booleans present in

a particular object. That's why it isn't static. If you do want to make it

a static method you'd have to pass the object to be printed as a parameter

to this method which would be clumsy at best.

kind regards,

Jos

JosAHa at 2007-7-9 21:33:06 > top of Java-index,Java Essentials,New To Java...
# 2

Hey

This is my understanding of static so far... I know it doesnt answer your question but, It helps me to understand a bit better, by more pros, telling me im wrong!

e.g. you create 3 objects of ABC class:

ABC abc1 = new ABC();

ABC abc2 = new ABC();

ABC abc3 = new ABC();

there are not 3 showBools() methods created within those objects

There is only 1 showBools() method, that is in your ABC class BUT it can be used by all abc1, abc2 and abc3 objects.

FatClienta at 2007-7-9 21:33:06 > top of Java-index,Java Essentials,New To Java...
# 3
thx 4 replysif the showBools() method is not a class method, then what is it called?
mark_8206a at 2007-7-9 21:33:06 > top of Java-index,Java Essentials,New To Java...
# 4
Instance method
manuel.leiriaa at 2007-7-9 21:33:06 > top of Java-index,Java Essentials,New To Java...