I have a User
(parent) with a Task
(child). I need a user instance within each task and want to have the task available within the user.
Are there any differences in version 1 and 2?
Which version should I prefer?
Version 1:
public class User {
// does this creates a new Task on each field access?
private Task task = new Task(this);
public void runTask() {
task.run();
}
}
Version 2:
public class User {
private Task task;
public getTask() {
if(task == null) {
task = new Task(this);
}
return task;
}
public void runTask() {
task.run();
}
}
I guess, the first version does an eager initializing and is the same as doing this:
Version 3
public class User {
public User(){
// I need the user instance to put into the task, but this is null here..
this.task = new Task(this);
}
private Task task;
}
What I want to achieve is:
I have a lot of methods within User.java
and want to outsource these methods to a new class Task.java
(with the relation to its user).