Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java publicstaticvoidmain()访问非静态变量_Java_Variables_Static Methods_Main_Void - Fatal编程技术网

Java publicstaticvoidmain()访问非静态变量

Java publicstaticvoidmain()访问非静态变量,java,variables,static-methods,main,void,Java,Variables,Static Methods,Main,Void,据说非静态变量不能在静态方法中使用。但是公共静态void main可以。这是怎么回事?不,它没有 public class A { int a = 2; public static void main(String[] args) { System.out.println(a); // won't compile!! } } 但是 或者如果您实例化A public class A { int a = 2; public static void main(String

据说非静态变量不能在静态方法中使用。但是公共静态void main可以。这是怎么回事?

不,它没有

public class A {
  int a = 2;
  public static void main(String[] args) {
    System.out.println(a); // won't compile!!
  }
}
但是

或者如果您实例化
A

public class A {
  int a = 2;
  public static void main(String[] args) {
    A myA = new A();
    System.out.println(myA.a); // this works too!
  }
}


将起作用,因为
a
在这里是局部变量,而不是实例变量。方法局部变量在方法执行期间始终是可访问的,无论该方法是否是静态的。

主方法也不能访问非静态成员

final public class Demo
{
   private String instanceVariable;
   private static String staticVariable;

   public String instanceMethod()
   {
      return "instance";
   }

   public static String staticMethod()
   {
      return "static";
   }

   public static void main(String[] args)
   {
      System.out.println(staticVariable); // ok
      System.out.println(Demo.staticMethod()); // ok

      System.out.println(new Demo().instanceMethod()); // ok
      System.out.println(new Demo().instanceVariable); // ok

      System.out.println(Demo.instanceMethod()); // wrong
      System.out.println(instanceVariable);         // wrong 
   }
}
这是因为在默认情况下,当您调用方法或变量时,它实际上是在访问
This.method()
或This.variable。但是在main()方法或任何其他静态方法()中,尚未创建“this”对象

从这个意义上讲,静态方法不是包含它的类的对象实例的一部分。这就是实用程序类背后的想法

要在静态上下文中调用任何非静态方法或变量,需要首先使用构造函数或工厂构造对象,就像在类之外的任何地方一样


更深入:

基本上,这是JavaIMO设计中的一个缺陷,它允许静态成员(方法和字段)像引用实例成员一样被引用。这样的代码可能会非常混乱:

Thread newThread = new Thread(runnable);
newThread.start();
newThread.sleep(1000);
Thread newThread = new Thread(runnable);
newThread.start();
Thread.sleep(1000);
这看起来像是在让新线程进入睡眠状态,但实际上它编译成如下代码:

Thread newThread = new Thread(runnable);
newThread.start();
newThread.sleep(1000);
Thread newThread = new Thread(runnable);
newThread.start();
Thread.sleep(1000);
因为
sleep
是一种静态方法,它只会使当前线程处于睡眠状态

事实上,甚至没有检查变量的非空性(我相信,以前是这样的):


有些IDE可以配置为对这样的代码发出警告或错误-您不应该这样做,因为这样会损害可读性。(这是C#…)纠正的缺陷之一)

是的,主要方法可以访问非静态变量,但只能通过实际实例间接地

例如:

public class Main {
    public static void main(String[] args) {
        Example ex = new Example();
        ex.variable = 5;
    }
}

class Example {
    public int variable;
}
当人们说“非静态变量不能在静态方法中使用”时,他们的意思是同一类的非静态成员不能被直接访问(例如,如所示)

相关问题:


更新:

当谈到非静态变量时,一隐含地意味着成员变量。(因为局部变量无论如何都不可能有静态修饰符。)

在代码中

public class A {
    public static void main(String[] args) {
        int a = 2;
        System.out.println(a); // this works!
    }
}

您正在声明一个局部变量(通常不称为非静态变量,即使它没有静态修饰符)。

您可以在静态方法中创建非静态引用,如:

static void method() {
   A a = new A();
}

对于
publicstaticvoidmain(String[]args)
method

**您可以在这里看到一个表,该表清除了静态和非静态方法中静态和非静态数据成员的访问**

静态方法不能访问非静态字段,但您当然可以在方法本身中定义局部变量(并通过这些变量访问非静态字段)。请举例说明.public类A{public static void main(String[]args){int A=2;System.out.println(A);//这个有效!}}我的问题是它的公共类A{public static void main(String[]args){int A=2;System.out.println(A);//这个有效!}'这不使用任何Instanceso,其中包含静态方法和变量(如果不在定义它们的类中)?因此main访问instanceMethod和instanceVariable的唯一方法是将其作为演示类的对象?如果
instanceMethod
instanceVariable
是实例方法和实例变量然后,它们只能通过各自类的对象解除对它们的引用来从静态方法访问。
static void method() {
   A a = new A();
}
public class XYZ
{
   int i=0;
   public static void increament()
       {
       i++;   
       }
}
public class M
{
    public static void main(String[] args)
    {
    XYZ o1=new XYZ();
    XYZ o2=new XYZ();
    o1.increament(); 
    XYZ.increament(); //system wont be able to know i belongs to which object 
                  //as its increament method(static method)can be called using   class name system 
                  //will be confused changes belongs to which object.
    }
}