javaobjectinner-classesouter-classes

Access object created in innerClass from elsewhere java


so i have outerClass and innerClass, and i want to access the object created from innerClass using outerClass, example:

public Class outerClass{
    Class innerClass{
        //properties
    }
}

so what i want to do is something like this:

public class Main {
    public static void main(String [] args) {
        outerClass outerObj = new outerClass();
        outerClass.innerClass innerObj = outerObj.new innerClass();

        //this is what i want:
        outerObj.innerObj;
    }
}

it might be complicated but what i want to do is get the innerObject, using only outerObject


Solution

  • public Class outerClass{
        Class innerClass{
            //properties
        }
    
        public innerClass innerObj;
    }
    
    outerObj.innerObj = outerClass.new innerClass();