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<>();
    TimerTask task = new TimerTask() {
        public void run() {
             // task to run goes here

What I have tried:

I have tried:


But I can't get access to them.
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