I have a ListView in an Activity and I am setting a custom adapter to the ListView.
Should my adapter class be:
private static class MyAdapter extends ArrayAdapter
or
private class MyAdapter extends ArrayAdapter
I guess it should make no difference as long as the adapter is enclosed within the activity reference but wanted to confirm that.
So the inner class must either itself be static (in which case no owning instance is required), or you only create the inner class instance from within a non-static context.
Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.
Note: We can not have a static method in a nested inner class because an inner class is implicitly associated with an object of its outer class so it cannot define any static method for itself.
Java static nested class A static class is a class that is created inside a class, is called a static nested class in Java. It cannot access non-static data members and methods. It can be accessed by outer class name.
Holding onto the context is fine from within an adapter if you are careful about how you use the adapter. Adapters are usually tied to the lifecycle of their Context (an Activity) so it's fine. Use a WeakReference only if it makes sense.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With