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
TimeOnlineInfoandUserDataare in a strict 1-to-1 relationship (as they appear to be), why have a separate, publicly visibleTimeOnlineInfoclass in the first place? There could be reasons for that of course, but from what you posted, the easiest solution would be to get rid ofTimeOnlineInfoaltogether.