Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is class declared as static in Java? [duplicate]

I've seen class been declared as static in java, but confused:
Since class is used to create objects, and different objects have different memory allocations.
Then what is "static" used for when declaring a class?Does it mean the member variables are all static?
Does this make sense?

like image 475
Al2O3 Avatar asked Mar 16 '13 10:03

Al2O3


People also ask

Why we declare class as static in Java?

In Java, static is a keyword that can be used with variables, classes, blocks, and methods. When we use the static keyword before any of them, it means that specified member belongs to a type itself. In other words, an instance of a static member is created and shared across all the instances of the class.

Why static is single copy in Java?

In Java, when we declare a field static, exactly a single copy of that field is created and shared among all instances of that class. It doesn't matter how many times we initialize a class. There will always be only one copy of static field belonging to it.

Why static is always single copy?

Only one copy of a static member exists, regardless of how many instances of the class are created. Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.

What is the use of declaring a class as static?

We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. A static member is shared by all objects of the class.


1 Answers

Firstly you cannot make top-level-class static. you can only make a nested class static. By making an nested class static you basically are saying that you don't need an instance of the nested class to use it from your outer class/top-level-class.

Example:

class Outer {

static class nestedStaticClass {

//its member variables and methods (don't nessarily need to be static)
//but cannot access members of the enclosing class
}

public void OuterMethod(){
//can access members of nestedStaticClass w/o an instance
}
}

Also to add, it is illegal to declare static fields inside an inner class unless they are constants (in other words, static final). As a static nested class isn't an inner class you can declare static members here.

Can class be nested in nested class?

In a word, yes. Look at the below Test, both nested inner classes and nested static class can have nested classes in 'em. But remember you can only declare a static class inside a top-level class, it is illegal to declare it inside an inner class.

public class Test {
    public class Inner1 {
        public class Inner2 {
            public class Inner3 {

            }
        }
    }
    public static class nested1 {
        public static class nested2 {
            public static class nested3 {

            }
        }   
    }
}
like image 185
PermGenError Avatar answered Oct 25 '22 23:10

PermGenError