简体   繁体   English

匿名线程类无法访问非静态实例变量

[英]Anonymous thread class not able to access non static instance variables

I am trying to access instance variable inside a Thread anonymous class . 我正在尝试访问Thread匿名类内的实例变量。 I am getting an error here saying to make it static . 我在这里说要使其静态化时出现错误。 The point here is if i can access "this" keyword inside the anonymous class which treats it as its current object holder, then why is it not able to access the instance variables in a non static way . 这里的重点是,如果我可以访问将其视为当前对象持有者的匿名类中的“ this”关键字,那么为什么它不能以非静态方式访问实例变量。

public class AnonymousThreadDemo {
    int num;

    public AnonymousThreadDemo(int num) {
        this.num = num;
    }

    public static void main(String[] args) {
        Thread thread = new Thread() {
            @Override
            public void run() {
                System.out.println("Anonymous " + num); // Why cant we access num instance variable
                System.out.println("Anonymous " + this); // This can be accessed in a nonstatic way
            }
        };
        thread.start();
    }
}

num is a non static field, it belongs to a specific instance. num是一个non static字段,它属于特定实例。 You can not reference it in static main directly, because a static method can be called without creating an instance. 您不能直接在static main引用它,因为可以在不创建实例的情况下调用static方法。

this is actually referencing thread , it is a local variable, when you execute run , the thread must have been created. this实际上是在引用thread ,它是一个局部变量,当您执行run ,必须已创建thread

If you try reference AnonymousThreadDemo.this in main , you will get same result: 如果尝试在main引用AnonymousThreadDemo.this ,则会得到相同的结果:

public static void main(String[] args) {
    Thread thread = new Thread() {

        @Override
        public void run() {
            System.out.println("Anonymous " + AnonymousThreadDemo.this); // compile error
        }
    };
    thread.start();
}

This is ok, you can reference a local variable in local class: 可以,您可以在本地类中引用本地变量:

public static void main(String[] args) {
    int num = 0;

    Thread thread = new Thread() {
        @Override
        public void run() {
            System.out.println("Anonymous " + num);
        }
    };
    thread.start();
}

This is ok, you can reference a non static local class field in its method: 可以,您可以在其方法中引用一个非静态的本地类字段:

public static void main(String[] args) {
    Thread thread = new Thread() {
        int num = 0;

        @Override
        public void run() {
            System.out.println("Anonymous " + num);
        }
    };
    thread.start();
}

Check this for more. 检查这个更多。

num is non static that means it will come after static main in the memory. num是非静态的,这意味着它将在内存中的静态main之后。 Hence when main will try to point num it won't be available in the memory ie. 因此,当main将尝试指向num ,它将在内存中不可用,即。 It still won't be declared yet. 它仍然不会被宣布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以从另一个类访问非静态类的实例 - is it possible to access an instance of a non static class from another class 为什么我无法通过实例访问静态内部类 - Why am I not able to access static inner class through an instance 非静态嵌套线程-来自另一个类的访问(Java) - Non-static nested thread - access from another class (Java) 访问匿名内部类变量 - Access anonymous inner class variables 最终(常量)实例(非静态)变量是否像类(静态)变量一样? - Do final (constant) instance (non-static) variables act like class (static) variables? 多个线程中的实例对象会访问相同的 static class 和变量吗? - Will instance objects in multiple threads have access to the same static class and variables? 可以在静态方法中实例化的匿名内部类是否可以访问包含类的实例成员? - Can Anonymous inner class instantiated within a static method has access to instance members of containing class? 如何从不同的类访问非静态变量和方法 - How to access non-static variables & methods from a different class 如何从匿名类中访问封闭的类实例变量? - How can I access enclosing class instance variables from inside the anonymous class? 使用 javassist 匿名内部 class 时如何访问外部 class 的实例变量? - When using javassist anonymous inner class how to access instance variables of outer class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM