• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use of dot operator with variables (page 90, Head First Java)  RSS feed

 
Emily Shephard
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I'm a total beginner when it comes to Java (and all computer programming, in fact ), so this is probably a really simple question. I've starting working through Head First Java, and the Mixed Messages exercise in Chapter 4 is really confusing me. The thing that is completely throwing me off is the use of the dot operator with a variable.

I understand the use of dot operator with reference variables and methods that was covered earlier in the book (I think), but I do not understand how the dot operator is being used in this case with the variable counter.

Please excuse my terminology, formatting, and such; like I said, I'm a total beginner!

Thanks a bunch!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emily,
Welcome to CodeRanch!

Let's make the code a little simpler:


The first line creates a new reference variable. The second line access the counter field in that object, adds one to it and sticks it back into the field.

Your example is a little more complicated to read because it uses an array. Keep in mind

is equivalent to


Make sense? If not, which part is confusing so we can elaborate.
 
Gautam Joshi
Greenhorn
Posts: 18
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Emily..

Here, you are accessing instance variable counter of object m4a of calss Mix4.

Here, in this while loop total 9 instances has been instantiated via statement : " m4a[x] = new Mix4(); "

and then,

the instance, which is just created, accessed it's own instance variable counter via dot operator and increment it by one.
Here, through out the completion of while loop,
ALL NINE instances' instance member, counter, has been accessed and incremented +1 by its previous counter value(in this case it is zero for all).// m4a[x].counter = m4a[x].counter + 1;


for the first time:

m4a[0].counter is being incremented and then
m4a[1].counter... and so on..!!


You can always access your members (including, instance members and class members) via dot operator.

Class members: "These are the members who has been declared with 'static' access specifier in the class definition. To access this you need not need to create instance of class. "
Instance members: "These are the members who has been declared non-static, you always need to create a instance of a class, like here: Mix4[x] m4a = new Mix4();"


But, by the way,, can
 
Emily Shephard
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both for your responses!

I think I understand it now. is simply accessing the instance variable counter of the object m4a[x], which equals 0 regardless of x, and is adding 1 to it. Under different circumstances, the value of the instance variable (counter) for m4a[x] could change based on the value of x, right?

I'm not sure if that made any sense or is at all correct. Thank you so much for your help!

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emily Shephard wrote:I think I understand it now. is simply accessing the instance variable counter of the object m4a[x], which equals 0 regardless of x, and is adding 1 to it. Under different circumstances, the value of the instance variable (counter) for m4a[x] could change based on the value of x, right?

Exactly!
 
Emily Shephard
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok! Thanks again for all your help!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!