Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By pbrockway2
  • 1 Post By quad64bit

Thread: What does static mean?

  1. #1
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default What does static mean?

    I am studying Java in school right now. I am trying to figure out what the keyword static does. What does it do? What does it mean to declare something static e.g. a method?

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

    Default Re: What does static mean?

    Java Code:
    String foo;
    String is a class, or type of thing. foo is a particular thing; an instance of String.

    "static" can be applied to variables, methods and blocks of code outside of methods and constructors. In each case it is used to show that the variables, methods and code belong to the class and not the instance. In effect static variables are shared by all the instances of a class.

    There is an example of them on the Understanding Instance and Class Members page of Oracle's Tutorial.

    A good rule of thumb is to use "static" only for the main() method of your application. (And that's a rather subtle use of static: the point is not so much that main() is shared, but rather that there is no instance of the class at the time that main() is run.)
    sunde887 likes this.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: What does static mean?

    I agree with pbrockway2. There are very few instances where you want to use static, and the valid cases are generally very specific. An example of a good place to use it might be if you're defining global variables (especially when paired with 'final' as in unchanging).

    Math.PI or Math.E or Color.RED are all good examples of static things - You can access and use them without an instance of Math or Color, and in these cases they are also final (cannot be changed). If you find yourself using static for anything else and aren't an advanced programmer, you're probably doing something wrong. In all my code I probably use static variables/methods 1% of the time.

  4. #4
    Kuttappu's Avatar
    Kuttappu is offline Member
    Join Date
    Aug 2011
    Location
    Kerala, India
    Posts
    40
    Rep Power
    0

    Default Re: What does static mean?

    If u declare a variable or method as "static" it means, that variable or method is a "common" accessible memory location for all components in your program which can access that variable.
    Static variables of a class are "common" to all objects of the class unlike other variables which we refer by "obj.var" (where "obj" is an object/instance of the class and "var" is the variable of that instance).

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: What does static mean?

    unlike other variables which we refer by "obj.var" (where "obj" is an object/instance of the class and "var" is the variable of that instance).
    Wich are known as 'instance variables' :)
    Kuttappu likes this.

Similar Threads

  1. Replies: 9
    Last Post: 06-10-2011, 01:40 AM
  2. Replies: 7
    Last Post: 09-24-2010, 06:09 AM
  3. Replies: 4
    Last Post: 05-12-2010, 12:05 PM
  4. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Posting Permissions

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