Click here to Skip to main content
14,325,629 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a class named MyHarddriveStorage which I make instances of that in my other class named MyMainClass. I have an inner class inside MyMainClass class. How can I get accessed to my hds1, hds2 and MyHarddriveStorageList variables from inner class?

public class MyMainClass {
    MyHarddriveStorage hds1 = new MyHarddriveStorage();
    MyHarddriveStorage hds2 = new MyHarddriveStorage();
    
    LinkedList<MyHarddriveStorage> MyHarddriveStorageList = new LinkedList<>();
    MyHarddriveStorageList.add(hds1);
    MyHarddriveStorageList.add(hds2);
    
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
             // task to run goes here
        }
    };


What I have tried:

I have tried:
super.hds1

MyMainClass.this.hds1

But I can't get access to them.
Posted
Updated 10-Jun-19 10:00am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Pass a reference to the instance of MyMainClass to the "inner class" via a constructor, or method / initializer.

Using dependency injection in Java - Introduction - Tutorial[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100