Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

static method ?

Thread view: 
Mike - 17 Apr 2007 07:52 GMT
Hi

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

thanks.

Mike
Daniel Pitts - 17 Apr 2007 08:03 GMT
> Hi
>
[quoted text clipped - 5 lines]
>
> 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.
Ian Wilson - 17 Apr 2007 10:37 GMT
> 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.
Chris Dollin - 17 Apr 2007 11:13 GMT
> 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.

Signature

"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

CodeForTea@gmail.com - 17 Apr 2007 13:02 GMT
> > I'm a newbie in Java.
> > What's the difference between  "public method" & "public static
[quoted text clipped - 10 lines]
> 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));

      }

}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.