美文网首页
static class vs non-static class

static class vs non-static class

作者: 成江 | 来源:发表于2017-12-10 23:15 被阅读11次

A class can be made static only if it is a nested class.

  1. Nested static class doesn’t need reference of Outer class
  2. A static class cannot access non-static members and methods of the Outer class
    It can be accessed by outer class name. It can access static data members of outer class including private.

Java allows us to define a class within another class. Such a class is called a nested class. The class which enclosed nested class is known as Outer class. In java, we can’t make Top level class static. Only nested classes can be static.

What are the differences between static and non-static nested classes?
Following are major differences between static nested class and non-static nested class. Non-static nested class is also called Inner Class.

  1. Nested static class doesn’t need reference of Outer class, but Non-static nested class or Inner class requires Outer class reference.

  2. Inner class(or non-static nested class) can access both static and non-static members of Outer class. A static class cannot access non-static members of the Outer class. It can access only static members of Outer class.

  3. An instance of Inner class cannot be created without an instance of outer class and an Inner class can reference data and methods defined in Outer class in which it nests, so we don’t need to pass reference of an object to the constructor of the Inner class. For this reason Inner classes can make program simple and concise.

/* Java program to demonstrate how to 
implement static and non-static classes in a java program. */
class OuterClass{
   private static String msg = "GeeksForGeeks";
    
   // Static nested class
   public static class NestedStaticClass{
      
       // Only static members of Outer class is directly accessible in nested 
       // static class 
       public void printMessage() {
 
         // Try making 'message' a non-static variable, there will be 
         // compiler error  
         System.out.println("Message from nested static class: " + msg); 
       }
    }
    
    // non-static nested class - also called Inner class
    public class InnerClass{
        
       // Both static and non-static members of Outer class are accessible in 
       // this Inner class
       public void display(){
          System.out.println("Message from non-static nested class: "+ msg);
       }
    }
} 
class Main
{
    // How to create instance of static and non static nested class?
    public static void main(String args[]){
        
       // create instance of nested Static class
       OuterClass.NestedStaticClass printer = new OuterClass.NestedStaticClass();
        
       // call non static method of nested static class
       printer.printMessage();   
  
       // In order to create instance of Inner class we need an Outer class 
       // instance. Let us create Outer class instance for creating 
       // non-static nested class
       OuterClass outer = new OuterClass();        
       OuterClass.InnerClass inner  = outer.new InnerClass();
        
       // calling non-static method of Inner class
       inner.display();
        
       // we can also combine above steps in one step to create instance of 
       // Inner class
       OuterClass.InnerClass innerObject = new OuterClass().new InnerClass();
        
       // similarly we can now call Inner class method
       innerObject.display();
    }
}

相关文章

  • static class vs non-static class

    A class can be made static only if it is a nested class. ...

  • Non-Static Inner Class

    非静态内部类(后面讨论的都是非静态的)能够获取到外部包裹类的私有的字段/方法因为: ...

  • tips

    条款5说过,class析构函数(无论是编译器生成的,还是用户自定义的)会自动调用其non-static成员变量(本...

  • swift 中 Class 和 Static 的区别

    Class 和 Static 都表示的是类方法。 无须初始化,Class 和 Static 的区别是 Class ...

  • static、class

    类型区别class 关键字专门用在 class 类型的上下文中的,可以用来修饰类方法以及类的计算属性(注意:不能用...

  • class static

    类class通过 static 关键字定义静态方法。不能在类的实例上调用静态方法,而应该通过类本身调用。不需要实例...

  • java 单利模式

    public class Singletion { private static class InnerSingl...

  • fibonacci数列的两种实现

    public class MyFibonacci { private static class GenerateT...

  • c++ static 成员变量

    一 static变量在class中的使用 class中声明static变量,s_value的存在不依赖class的...

  • Java重载(OverLoad)的理解

    ``` public class Test{ static abstract class Human{ } sta...

网友评论

      本文标题:static class vs non-static class

      本文链接:https://www.haomeiwen.com/subject/zrdbixtx.html