Coming from a JavaScript background, I find the following code a bit too robust, containing multiple statements; and am wondering how I can simply the code, and do everything in a single statement.
Student is the superclass and Friend and Schedule are the subclasses aggregated into a superclass ArrayList public member (these are not nested classes). Here is my current code:
public static void main(String[] args) {
// Subclass1 instantiation and declaration
Friend friend_of_andy_1 = new Friend(1002, "Bob");
ArrayList<Friend> andys_friends = new ArrayList<>();
andys_friends.add(friend_of_andy_1); // superclass member
// Subclass1 instantiation and declaration
Schedule schedule_of_andy_1 = new Schedule(1, "Yoga practice");
ArrayList<Schedule> andys_schedule = new ArrayList<>();
andys_schedule.add(schedule_of_andy_1); // superclass member
// Superclass instantiation declaration with subclass objects in constructor argument
Student student_andy = new Student(1001, "Andy", andys_friends, andys_schedule);
}
I'm wondering if I can do something like this instead, where I declare/instantiate the superclass and subclasses in a single statement; is this possible?
public static void main(String[] args) {
Student student_andy = new Student(
1001,
"Andy",
// instantiation of subclass
ArrayList<Friend>[
Friend(
{
1002,
"Bob"
}
)
],
ArrayList<Schedule>[
Schedule(
{
1,
"Yoga practice"
}
)
]
)
};