What does static mean in Java?

static is a keyword used with variables, methods, inner classes and code blocks.

If it is used with :

  • Method or variable : that means this method or variable doesn’t belong to a particular instance, it belongs to the class where it is declared. We can use it without creating an instance of its class : ClassName.methodName() or ClassName.variableName. We can manipulate the static methods and variables via the class instance, and if we want to make a static variable a constant we use the keyword final.
  • Code block : that means this code block will be executed once when the class is loaded. This technique is used if we want to initialize some variables during the construction of an object.
  • Inner class : called also static nested class, all simply it is a class declared within an other class. to use a method of  a inner class we invoke it by instancing this class and we call this method. Example :
public class MainClass {
    private String privateField; 
    public static classStaticNestedClassExample {
       public void methodExample() {
           // code ...

To use methodExample():

MainClass.StaticNestedClassExample instance = new MainClass.StaticNestedClassExample();


See also :

Share it with your freinds on Facebook Tweet it to your freinds on Twitter Myspace Friendfeed Technorati del.icio.us Digg Google Yahoo Buzz StumbleUpon

1 comment

No ping yet

  1. Car repos says:

    Nice work with the layout of the blog – it is very easy on the eye

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

WP SlimStat