javaconstructorinner-classesnested-classouter-classes

Create Instance of a Class that is Used in the Constructor of its Outer Class


I'm trying to create an instance of a class that is used in the constructor of its outer class. Referring to the code below, I need a UserData object but I also need a TimeOnlineInfo object to create it, and I don't see a way of getting TimeOnlineInfo object without first having an instance of UserData because TimeOnlineInfo is not static. I can't make it static though because it needs to access a method from its outer class. Is there anyway I could get this to work or get the most similar effect? I did realize that I could just make the classes static and not save the data directly in the addTime method but i was already halfway through this question and I'm curious to see if there is a way of doing this.

Here is a very simplified version of my code:

class UserData {
    TimeOnlineInfo timeOnline;

    public UserData(Object data1, Object data2, Object data3, Object data4, Object data5, TimeOnlineInfo timeOnlineInfo){
        this.timeOnlineInfo = timeOnlineInfo;
    }

    public class TimeOnlineInfo {
        private int time;

        public TimeOnlineInfo(int time){
           this.time = time;
        }

        public void addTime(int time){
            this.time += time;
            UserData.this.saveData();
        }
    }
}


UserData userData = new UserData(new UserData.TimeOnlineInfo());//Doesn't work because PlayInfo is not a static class
UserData userData = new UserData(userData.new TimeOnlineInfo());//This is just a stupid because i'm using the uncreated object in its own constructor

Solution

  • You've got some general confusion about a few things. Let's start at the beginning.

    First, this isn't a constructor.

    public void UserData(TimeOnlineInfo timeOnlineInfo){
        this.timeOnlineInfo = timeOnlineInfo;
    }
    

    You meant to drop the void declaration.

    public UserData(TimeOnlineInfo timeOnlineInfo){
        this.timeOnlineInfo = timeOnlineInfo;
    }
    

    Second, it doesn't make structural sense to instantiate the outer class with an instance of the inner class, since the only way to get an instance of the inner class is to use the outer class to begin with.

    For example, you would have to use new UserData().new TimeOnlineInfo(int) for an instance of TimeOnlineInfo, but that's only:

    If you really want to keep this design, then consider passing in an int to your construtor instead, so it can be supplied to the instance of TimeOnlineInfo.

    class UserData {
        TimeOnlineInfo timeOnlineInfo;
    
        public void saveData() {
            // stub
        }
    
        public UserData(int value) {
            this.timeOnlineInfo = new TimeOnlineInfo(value);
        }
    
    
        public class TimeOnlineInfo {
            private int time;
    
            public TimeOnlineInfo(int time){
                this.time = time;
            }
    
            public void addTime(int time){
                this.time += time;
                UserData.this.saveData();
            }
        }
    }