Assume we have a class:
class Account {
String name;
int ID;
}
Then
a1 = new Account();
a2 = new Account();
Will create 2 variables that point to 2 memory locations storing 2 instances of class Account.
My question is how Java can know how big these instances are to allocate their memory (Because with String type, we can assign any string to that. For example, a1.name = "Solomon I", a2.name = "Alan". This will lead to different size of each instance)
Memory location is a 'continuous' string of bytes. Therefore, if I have a1 = new Account() then a2 = new Account() => a1's memory location is fixed ('used memory | a1 | a2') so what will happen if I make a1.name a very long string? Will a1's memory location extend to a2's memory location?
Thank you for reading this, please let me know if I have any misconception.