D. Joe
D. Joe

Reputation: 19

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

instance.methodX(100)

from inner class it is leading to memory leak. If I remove static keyword from inner class Broadcast receiver not fired.

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.

Upvotes: 0

Views: 870

Answers (2)

Zoe - Save the data dump
Zoe - Save the data dump

Reputation: 28288

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.

When it is static, you don't need an object of the outter class to work.

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. I.e.

new Outter.Inner(outterInstance)

However: Since the class extends BroadcastReceiver, it initializes into an empty constructor. Meaning the constructor with values to pass will never be used, since BroadcastReceiver is a system-initialized and handled system. 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:

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.

You should read this SO post on the topic

Upvotes: 1

Abdul Waheed
Abdul Waheed

Reputation: 4678

You can just mark that method methodX() as static. After this you will be able to access that method in you static inner class.

Upvotes: 1

Related Questions