static method ?

M

Mike

Hi

I'm a newbie in Java.
What's the difference between "public method" & "public static
method"?

thanks.


Mike
 
D

Daniel Pitts

Hi

I'm a newbie in Java.
What's the difference between "public method" & "public static
method"?

thanks.

Mike

While I don't mind answering a few newbie questions, I suggest you
start READING the book that you so far seem to be SCANNING.

Last answer from me for your newbie questions.

they are both accessible to all other classes, but the static method
is not associated with any particular instance of that class, and can
be called without a reference to an instance of that class.
 
I

Ian Wilson

Mike said:
I'm a newbie in Java.
What's the difference between "public method" & "public static
method"?

You may find it better to use a search engine for these sorts of simple
question:

http://www.google.com/search?q=static+method

The first two items listed provide an explanation with examples. I
suspect you are unlikely to get a better answer here.

If you Googled and still have questions, ask here (or perhaps better in
in comp.lang.java.help) but mention that you tried googling, so that
people here may be less irritated by a seeming lack of effort on your part.
 
C

Chris Dollin

Mike said:
I'm a newbie in Java.
What's the difference between "public method" & "public static
method"?

(a) Doesn't your Java book tell you?

(b) A "public static" method is both public and static. A method that
is "public" but not "static" is public but not static.
 
C

CodeForTea

(a) Doesn't your Java book tell you?

(b) A "public static" method is both public and static. A method that
is "public" but not "static" is public but not static.

--
"Based on their behaviour so far -- I have no idea." /Sahara/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

public class TestStatic {
int value;

public TestStatic(int v) {
value = v;
}

public int getCubeCalue() {
return value * value *value;
}

public static int getSquareValue(int i) {
return i * i;
}

public static void main(String[] args ) {
int i = 5;
TestStatic test = new TestStatic(i);
// in calling the getCubeCalue I did use test
// (instance of TestStatic)
// for invoking non static methods.
System.out.println("Cube value of " + i
+ " = " + test.getCubeCalue());
i = 10;
// in calling the getSquareValue I did not use test
// (instance of TestStatic)

System.out.println("Square value of " +
" = " + TestStatic.getSquareValue(i));


}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top