Results 1 to 7 of 7
  1. #1
    Ray
    Ray is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default What does static mean when declaring a method?

    Okay, so when declaring a variable, static (as I understand it) means it is a class variable and does not belong to, nor is including in, the fields for an instance of that class.

    Now, what does the word static in the declaration of a method do?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: What does static mean when declaring a method?

    It's the same thing: it means that the method represents a class method and does not belong to, nor is included in, the behaviour of an instance of that class. Put another way, it's behaviour that an instance shares with all other instances of the class, much as static variables are shared.

    Since a static method is shared behaviour it can only access shared variables.

  3. #3
    Ray
    Ray is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default Re: What does static mean when declaring a method?

    I didn't get the "put another way" part, but I sort of understand from your first point.

    Uhm what do you mean by, static methods are shared behaviour? What does behaviour mean anyway? I learned methods are the way we interact with the object. So how can we have a method that doesn't allow a user to interact with an object. Can you give me an example?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: What does static mean when declaring a method?

    The main() method is commonly a static method.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: What does static mean when declaring a method?

    Uhm what do you mean by, static methods are shared behaviour? What does behaviour mean anyway? I learned methods are the way we interact with the object.
    I threw "behaviour" in there deliberately.

    All I mean by it is ... behaviour. Dogs return a stick, people return a kindness, instances of String return their length. In each case "behaviour" is just the things they do. It's true that objects are interacted with by means of their methods but, significantly, that interaction always causes the objects to do something. It's behaviour. (Not all behaviour involves returning anything though.)

    Looking more closely at the length() method of String the API docs state: "Returns the length of this string". Notice how it begins with a verb because it is describing a behaviour: it's something a string does. Notice also how the description refers to "this string". That means that the behaviour is specific to the particular instance of String (this one doesn't return that one's length). In other words length() is a non static method.

    Compare that with the description of the static method valueOf(boolean) which says "Returns the string representation of the boolean argument". So what this method does is, eg, if you pass it -1<42 it will return the string "true". What it does is not so important. What is important is that it is not really behaviour of this or that string in particular. All strings will return "true" when this method is passed -1<42. Unlike the other case, the API description does not mention "this string". In short valueOf() is a static method: a behaviour (of returning something) that all strings share.

  6. #6
    certchamp is offline SCJP/OCPJP Exam materials
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Default Re: What does static mean when declaring a method?

    It is a method which belongs to the class and not to the object(instance)
    A static method can access only static data. It can not access non-static data (instance variables)
    A static method can call only other static methods and can not call a non-static method from it.
    A static method can be accessed directly by the class name and doesn’t need any object
    Syntax : <class-name>.<method-name>
    A static method cannot refer to “this” or “super” keywords in anyway

  7. #7
    Ray
    Ray is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default Re: What does static mean when declaring a method?

    Okay, I understand. Thanks a lot!

Similar Threads

  1. Declaring a method inside a method: Runnable?
    By africanhacker in forum New To Java
    Replies: 8
    Last Post: 03-23-2011, 04:30 AM
  2. Replies: 7
    Last Post: 09-24-2010, 05:09 AM
  3. Replies: 3
    Last Post: 02-09-2010, 05:22 AM
  4. Replies: 1
    Last Post: 02-10-2009, 10:03 AM
  5. Replies: 1
    Last Post: 08-07-2007, 05:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •