小编use*_*411的帖子

为什么不能在方法内创建非静态类的静态对象?

我可以看到非静态类的静态对象不能在方法内创建?

码:

  public class Rent {

      public void abc() {
          System.out.println("Non static method");
      }
      public static void def() {
          System.out.println("this is static method");
      }
  }

  public class SampleJava {

      public static void main(String[] args) {
          Rent r1 = new Rent();
          public static Rent r2; //not allowed in static method
      }

      public static Rent r3; //allowed outside method

      public void def() {
          Rent r4 = new Rent();
          public static Rent r5; //not allowed in non-static method either
      }
  }
Run Code Online (Sandbox Code Playgroud)

java static

1
推荐指数
1
解决办法
1246
查看次数

标签 统计

java ×1

static ×1