Reputation: 309
I'm revising my Java book to make sure i have solid understanding of objects and Java basics in general. I stumbled upon this piece of code from the book i was reading Head First: Java 2nd edition (2005)
class Book {
String title;
String author;
}
class Main {
public static void main(String args[]) {
int x = 0;
Book[] myBooks = new Book[3];
myBooks[0] = new Book();
myBooks[1] = new Book();
myBooks[2] = new Book();
myBooks[0].title = "Example title xx";
myBooks[1].title = "Example title cc";
myBooks[2].title = "Example title yy";
myBooks[0].author = "Example author xx";
myBooks[1].author = "Example author cc";
myBooks[2].author = "Example author yy";
while (x < 3) {
System.out.print(myBooks[x].title);
System.out.print(", author ");
System.out.println(myBooks[x].author);
x = x + 1;
}
}
}
I don't quite understand the syntax of myBooks[0].title = "Example title xx"
I admit i'm not quite experienced with arrays and how they work yet, but isn't it a better practice to loop through the array and set all the object fields with setter methods?
What i think is the case here
From my limited understanding, This particular method of assigning values to those fields is related to the scope of those two classes. The same way you'd use names of the static methods instead of first creating object of their respective classes, but with static variables instead.
It seems very trivial, but it's very important for me to understand and grasp the idea. I hope you can clear it up for me.
Upvotes: 1
Views: 12716
Reputation: 692
when you work with arrays , you should understand the concept.
Book[] mybooks = new Book[3]
This means there will be an array in Book type. Book[0]
means, this is the reference to the real object in memory(as like a remote control to the object).
So when you call the mybook[0].title
, it means you are calling the title method of Book[0]
(as like pressing the button title on your Book[0]'s remote control.
Upvotes: 1
Reputation: 726499
First, let's rewrite your program without an array:
Book myBooks0, myBooks1, myBooks2;
myBooks0 = new Book();
myBooks1 = new Book();
myBooks2 = new Book();
myBooks0.title = "Example title xx";
myBooks1.title = "Example title cc";
myBooks2.title = "Example title yy";
myBooks0.author = "Example author xx";
myBooks1.author = "Example author cc";
myBooks2.author = "Example author yy";
If you understand Java variables referencing cusom classes, this should be no mystery at all.
Next, let's go back to your array:
Book[] myBooks = new Book[3];
It replaces three named variables above with one array variable, so now instead of writing myBooksX
we write myBooks[X]
.
That's most of the difference between the two. The advantage of an array is that X
can be a variable or an integer expression that evaluates to 0, 1, or 2, while with individual variables the number must be hard-coded.
Upvotes: 3
Reputation: 9687
myBooks[0].title = "Example title xx"
will assign the string to the title
field of the 1st element in the array
Isn't it a better practice to loop through the array and set all the object fields with setter methods?
Generally speaking yes it is.
Upvotes: 1