I am a rookie in Java Programming. I am clearing all my concepts before making a step ahead. I was reading an array chapter which says the basic structure to create an array is:
Type[] var_name = new Type[limit];
I was going through some slides of opencourseware. In these slides, they inserted a class name into the type of the array. For example:
public class Baby {
Baby[] siblings;
}
Can someone explain me what is the difference between the basic array structure and the structure written inside class.
I think this may just be confusion about what constitutes a type. For the reference:
Type[] var_name = new Type[limit]
"Type" must be replaced with any primitive type (int, double, etc.) as well as any Class (Baby, in your case), such as:
String [] string_array = new String[10];
If that's not the issue you're having, the other difference between the two statements is that the first actually creates an array of size "limit" and assigns it to the variable var_name... whereas in the Baby declaration, only the member variable "siblings" in the Baby class is declared. That variable can hold a Baby array, but that array has not yet been created. In the Baby constructor, you might see something like:
Baby() {
siblings = new Baby[100];
}
This would create an array of Baby class object references of size 100, and assign it to the siblings member of the instance of Baby being created.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With