简体   繁体   English

如何从Java中的静态内部类访问外部类的私有成员

[英]How to access private members of outer class from static inner class in Java

public  class OuterClass extends Something{

    private int unit = 0;

    private void methodX(int num){
        //Do something here
    }

     public static class InnerClass extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {     

        // Need to call methodX(100) here       
        }
     }      
}

I am crating an application and it works fine. 我正在创建一个应用程序,并且运行良好。 but when I am using 但是当我使用

static OuterClass instance;

in OuterClass and access its variable through 在OuterClass中并通过以下方式访问其变量

instance.methodX(100)

from inner class it is leading to memory leak. 内部类导致内存泄漏。 If I remove static keyword from inner class Broadcast receiver not fired. 如果我从内部类中删除static关键字,则不会触发广播接收器。

this is the part of my manifest file. 这是清单文件的一部分。

<receiver
    android:name=".OuterClass$InnerClass"
    android:enabled="true"
    android:exported="true">
    <intent-filter >
        <action android:name="com.xyz.abc.RESULT"/>        
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</receiver>

This is working as I expected but it has some memory leak. 这按我预期的那样工作,但是有一些内存泄漏。 I need to access outer class method from static inner class without a memory leak.for that I should avoid using static instance of outer class. 我需要从静态内部类访问外部类方法而不会发生内存泄漏。为此,我应该避免使用外部类的静态实例。

I am really grateful if anyone can help me to find a way to access outerClass methodX from inner class. 如果有人可以帮助我找到一种从内部类访问externalClass methodX的方法,我将非常感激。

Declaring a static inner class is similar to creating a new file, in the terms that you need an instance for the outter class for it to work. 声明静态内部类与创建新文件类似,在术语上,您需要外部类的实例才能使其工作。

Non-static inner classes work just like non-static fields: they're instance specific. 非静态内部类的工作方式与非静态字段类似:它们是特定于实例的。 Meaning you need an instance of the outter object to initialize a new inner object. 意味着您需要一个outter对象的实例来初始化一个新的内部对象。

When it is static, you don't need an object of the outter class to work. 当它是静态的时,您不需要outter类的对象即可工作。

For non-static inner classes, you can do this: 对于非静态内部类,您可以执行以下操作:

outter.new Inner()

and get access to the class that it's nested in. 并访问其嵌套的类。

But since your class is static, you have to pass an instance like normal. 但是由于您的类是静态的,因此您必须像平常一样传递一个实例。 Ie

new Outter.Inner(outterInstance)

However: Since the class extends BroadcastReceiver, it initializes into an empty constructor. 但是:由于该类扩展了BroadcastReceiver,因此它将初始化为一个空的构造函数。 Meaning the constructor with values to pass will never be used, since BroadcastReceiver is a system-initialized and handled system. 意味着将不会使用带有要传递的值的构造函数,因为BroadcastReceiver是系统初始化和处理的系统。 Create a new instance in the empty constructor instead, or move all the variables into what is currently the inner class 而是在空的构造函数中创建一个新实例,或将所有变量移至当前内部类中

Since BroadcastReceiver requires an empty constructor, doing this is not a possibility: 由于BroadcastReceiver需要一个空的构造函数,因此不可能这样做

public Inner(Outter instance)

It will not get initialized and you'll probably get exceptions from it too. 它不会被初始化,您也可能会从中获取异常。 You can, however, do this: 但是,您可以这样做:

public Inner(){
    outter = new Outter();
}

Or alternatively, move all the outter class fields and methods into the BroadcastReceiver. 或者,将所有外部类字段和方法移到BroadcastReceiver中。

You should read this SO post on the topic 您应该阅读有关主题的这篇SO帖子

You can just mark that method methodX() as static . 您可以将该方法methodX()标记为static After this you will be able to access that method in you static inner class. 之后,您将可以在您的static内部类中访问该方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM