Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.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中的误解_Java_Class_Static_Field_Non Static - Fatal编程技术网

Java中的误解

Java中的误解,java,class,static,field,non-static,Java,Class,Static,Field,Non Static,我正在尝试静态和非静态的方法和字段。 我试着编译这个: class main{ public int a=10; static int b=0; public static void main(String[] args){ b+=1; //here i can do anything with static fields. } } class bla { void nn(){ main.a+=1; //why here

我正在尝试静态和非静态的方法和字段。 我试着编译这个:

class main{
    public int a=10;
    static int b=0;
    public static void main(String[] args){
        b+=1; //here i can do anything with static fields.
    }
}

class bla {
    void nn(){
        main.a+=1; //why here not? the method is non-static and the field "main.a" too. Why?
    }
}
编译器返回给我:

try.java:10: non-static variable a cannot be referenced from a static context

但是为什么呢?方法和字段“a”都是非静态的

您需要类main的实例来更改a,因为它不是类变量。

您需要类main的实例来更改a,因为它不是类变量。

您没有在
nn()中初始化
main
的实例
方法。

您尚未在
nn()
方法中初始化
main
的实例。

您正在尝试以静态方式访问
a
。首先需要实例化
main
来访问
a

main m = new main();
m.a += 1;

此外,为了可读性,您应该将类名大写,并将实例变量用驼峰大小写。

您正在尝试以静态方式访问
a
。首先需要实例化
main
来访问
a

main m = new main();
m.a += 1;

此外,为了可读性,您应该将类的名称大写,并将实例变量用驼峰大小写。

变量
a
不是静态的,因此如果没有
Main

Main.b += 1; // This will work, assuming that your class is in the same package

Main main = new Main();
main.a += 1; // This will work because we can reference the variable via the object instance
那么,让我们假设我们有这个类

public class Main {

    public int a = 10;
    static int b = 0;

}
现在我们来看看,假设这些类在同一个包中

public class Blah {

    void nn() {

        Main.a += 1; // This will fail, 'a' is not static
        Main.b += 1; // This is fine, 'b' is static

        Main main = new Main();
        main.a += 1; // Now we can access 'a' via the Object reference

    }
}

变量
a
不是静态的,因此如果没有
Main的实例,就无法访问该变量

Main.b += 1; // This will work, assuming that your class is in the same package

Main main = new Main();
main.a += 1; // This will work because we can reference the variable via the object instance
那么,让我们假设我们有这个类

public class Main {

    public int a = 10;
    static int b = 0;

}
现在我们来看看,假设这些类在同一个包中

public class Blah {

    void nn() {

        Main.a += 1; // This will fail, 'a' is not static
        Main.b += 1; // This is fine, 'b' is static

        Main main = new Main();
        main.a += 1; // Now we can access 'a' via the Object reference

    }
}

newmain(新字符串[0])?真的吗?:-)糟糕的程序员@RichardSitze那是,我的大脑正式离开了大楼:P+1代表spot@sleax1.确保您使用的是更新后的示例,RichardSitze非常友好地为我同行审阅了我的示例。2. 
a
不是静态引用,如果没有
main
的实例,则无法访问它(并且两个类必须存在于同一个包中)。3.花点时间学习Java推荐的命名约定(),它将减少对类/实例引用的混淆:)
newmain(新字符串[0])?真的吗?:-)糟糕的程序员@RichardSitze那是,我的大脑正式离开了大楼:P+1代表spot@sleax1.确保您使用的是更新后的示例,RichardSitze非常友好地为我同行审阅了我的示例。2. 
a
不是静态引用,如果没有
main
的实例,则无法访问它(并且两个类必须存在于同一个包中)。3.花点时间学习Java推荐的命名约定()这将减少对类/实例引用的混淆:)使用以大写字母开头的类名的标准命名约定将真正有助于您的事业使用以大写字母开头的类名的标准命名约定我尝试过,但找不到符号:java:11:找不到符号symbol:variable m location:class main我试过了,但找不到symbol:java:11:找不到symbol:variable m location:class main