Static Classes In Java

后端 未结 13 1505
慢半拍i
慢半拍i 2020-11-22 07:37

Is there anything like static class in java?

What is the meaning of such a class. Do all the methods of the static class need to be static

相关标签:
13条回答
  • 2020-11-22 08:24

    Yes there is a static nested class in java. When you declare a nested class static, it automatically becomes a stand alone class which can be instantiated without having to instantiate the outer class it belongs to.

    Example:

    public class A
    {
    
     public static class B
     {
     }
    }
    

    Because class B is declared static you can explicitly instantiate as:

    B b = new B();
    

    Note if class B wasn't declared static to make it stand alone, an instance object call would've looked like this:

    A a= new A();
    B b = a.new B();
    
    0 讨论(0)
  • 2020-11-22 08:25

    What's happening when a members inside a class is declared as static..? That members can be accessed without instantiating the class. Therefore making outer class(top level class) static has no meaning. Therefore it is not allowed.

    But you can set inner classes as static (As it is a member of the top level class). Then that class can be accessed without instantiating the top level class. Consider the following example.

    public class A {
        public static class B {
    
        }
    }
    

    Now, inside a different class C, class B can be accessed without making an instance of class A.

    public class C {
        A.B ab = new A.B();
    }
    

    static classes can have non-static members too. Only the class gets static.

    But if the static keyword is removed from class B, it cannot be accessed directly without making an instance of A.

    public class C {
        A a = new A();
        A.B ab = a. new B();
    }
    

    But we cannot have static members inside a non-static inner class.

    0 讨论(0)
  • 2020-11-22 08:25

    All good answers, but I did not saw a reference to java.util.Collections which uses tons of static inner class for their static factor methods. So adding the same.

    Adding an example from java.util.Collections which has multiple static inner class. Inner classes are useful to group code which needs to be accessed via outer class.

    /**
     * @serial include
     */
    static class UnmodifiableSet<E> extends UnmodifiableCollection<E>
                                 implements Set<E>, Serializable {
        private static final long serialVersionUID = -9215047833775013803L;
    
        UnmodifiableSet(Set<? extends E> s)     {super(s);}
        public boolean equals(Object o) {return o == this || c.equals(o);}
        public int hashCode()           {return c.hashCode();}
    }
    

    Here is the static factor method in the java.util.Collections class

    public static <T> Set<T> unmodifiableSet(Set<? extends T> s) {
        return new UnmodifiableSet<>(s);
    }
    
    0 讨论(0)
  • 2020-11-22 08:26

    Seeing as this is the top result on Google for "static class java" and the best answer isn't here I figured I'd add it. I'm interpreting OP's question as concerning static classes in C#, which are known as singletons in the Java world. For those unaware, in C# the "static" keyword can be applied to a class declaration which means the resulting class can never be instantiated.

    Excerpt from "Effective Java - Second Edition" by Joshua Bloch (widely considered to be one of the best Java style guides available):

    As of release 1.5, there is a third approach to implementing singletons. Simply make an enum type with one element:

    // Enum singleton - the preferred approach
    public enum Elvis {
        INSTANCE;
        public void leaveTheBuilding() { ... }
    }
    

    This approach is functionally equivalent to the public field approach, except that it is more concise, provides the serialization machinery for free , and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks. While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton. (emphasis author's)

    Bloch, Joshua (2008-05-08). Effective Java (Java Series) (p. 18). Pearson Education.

    I think the implementation and justification are pretty self explanatory.

    0 讨论(0)
  • 2020-11-22 08:27

    You cannot use the static keyword with a class unless it is an inner class. A static inner class is a nested class which is a static member of the outer class. It can be accessed without instantiating the outer class, using other static members. Just like static members, a static nested class does not have access to the instance variables and methods of the outer class.

    public class Outer {
       static class Nested_Demo {
          public void my_method() {
              System.out.println("This is my nested class");
          }
       }
    public static void main(String args[]) {
          Outer.Nested_Demo nested = new Outer.Nested_Demo();
          nested.my_method();
       }
    }
    
    0 讨论(0)
  • 2020-11-22 08:28

    In simple terms, Java supports the declaration of a class to be static only for the inner classes but not for the top level classes.

    top level classes: A java project can contain more than one top level classes in each java source file, one of the classes being named after the file name. There are only three options or keywords allowed in front of the top level classes, public, abstract and final.

    Inner classes: classes that are inside of a top level class are called inner classes, which is basically the concept of nested classes. Inner classes can be static. The idea making the inner classes static, is to take the advantage of instantiating the objects of inner classes without instantiating the object of the top level class. This is exactly the same way as the static methods and variables work inside of a top level class.

    Hence Java Supports Static Classes at Inner Class Level (in nested classes)

    And Java Does Not Support Static Classes at Top Level Classes.

    I hope this gives a simpler solution to the question for basic understanding of the static classes in Java.

    0 讨论(0)
提交回复
热议问题