Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we make a class static in java? [duplicate]

Tags:

java

class

static

Possible Duplicate:
Static nested class in Java, why?

I know about the static fields in java. But don't know why we create a static class. Any idea?

like image 945
Khawar Raza Avatar asked Oct 21 '11 07:10

Khawar Raza


People also ask

Why we make a class static in Java?

In Java, static keyword is mainly used for memory management. It can be used with variables, methods, blocks and nested classes. It is a keyword which is used to share the same variable or method of a given class. Basically, static is used for a constant variable or a method that is same for every instance of a class.

Can you have multiple instances of a static class in Java?

A static class is a nested class (i.e. it is declared within another class). It behaves like a top level class, which means you can create multiple instances of it.

Why we call the static as a single copy storage?

Memory allocation for a static variable happens only once in the class area when the class is loaded in the memory. It is also known as a class variable. It is common to all the objects of the class. In this, a single copy of a static variable is created and shared among all the objects of the class.

When should you make a class static?

Use a static class as a unit of organization for methods not associated with particular objects. Also, a static class can make your implementation simpler and faster because you do not have to create an object in order to call its methods.

Why we create static methods?

A static method has two main purposes: For utility or helper methods that don't require any object state. Since there is no need to access instance variables, having static methods eliminates the need for the caller to instantiate the object just to call the method.

Can a static class have multiple objects?

When you declare a variable or a method as static, it belongs to the class, rather than a specific instance. This means that only one instance of a static member exists, even if you create multiple objects of the class, or if you don't create any.


2 Answers

class Outer {
    int a;
    class Inner {
        void test() {
            System.out.println(a);
        }
    }
}

Normally, an inner class may reference its owning class's fields, as you can see in the (pseudo) sample above. So an instance of Inner may access fields / methods / etc in Outer.

But this requires every Inner instance to have an Outer instance associated with it. So you cannot do

new Inner();

but instead must do

new Outer().new Inner();

In some cases this is not desirable - say you need to ensure that your Outer instances may get garbage collected, or that you need to make Inner instances independent of Outer. In that case you may change it to

static class Inner {

in which case now you may no longer reference "a" as before, but in exchange you don't need to have an instance of Outer at all to have instances of Inner.

like image 166
Steven Schlansker Avatar answered Sep 30 '22 23:09

Steven Schlansker


Top-level classes can't be declared static, only nested classes can be:

class OuterClass {
    ...
    static class StaticNestedClass {
        ...
    }
    class InnerClass {
        ...
    }
}

The difference is that:

  • non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private;
  • static nested classes do not have access to other members of the enclosing class.

The above is taken from the Java OO tutorial. You can read more here.

like image 45
NPE Avatar answered Sep 30 '22 22:09

NPE