jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes A little help for a total beginner? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A little help for a total beginner?" Watch "A little help for a total beginner?" New topic
Author

A little help for a total beginner?

Pierre Bungle
Greenhorn

Joined: May 05, 2009
Posts: 9
Hello hello,

Hope you're all well. This is my first post on this forum, but it was recommended by this textbook I stumbled upon (Head First Java). I find myself not understanding some really basic stuff, and have unfortunately run out of time to get it on my own. A little help would be immensely appreciated! Be patient, I'm a real doofus when it comes to coding... ;)

Here's the crux:



There are two things I don't understand:
1) is the first statement in the while loop creating a second array, in our hq array?
2) what is the statement hq[x].id = x actually doing (specifically the .id part)? I thought the . operator meant you were calling a method?

Thanks for your time!
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Please use code tags next time

1) is the first statement in the while loop creating a second array, in our hq array?


Nope, it is creating a new HeapQuiz object and assigning it to the first slot of your "hq" array which is declared to hold HeapQuiz objects. The "hq[x]" part of the statement is just accessing the "x" index (in this case 0) of your hq array while the "= new HeapQuiz();" part of your statement is creating a new object of type HeapQuiz and is returning a refernence to this object into your array.

2) what is the statement hq[x].id = x actually doing (specifically the .id part)? I thought the . operator meant you were calling a method?


Yes, the .id is accessing the member variable named "id" that belongs to the HeapQuiz object. While it is true you can use the dot operator to access methods, you can also access member variables as long as they are visible in your program. What I mean by visible is you could not access "id" using the dot operator if "id" was declared private.

Hope that helps, and welcome to the JavaRanch!


SCJA
~Currently preparing for SCJP6
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
A good practice exercise would be to figure out what your hq array would look like after the while loop is finished (don't peek at the book).

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Welcome to JavaRanch

I have added CODE tags so you can see how much better the post looks. But please use thread titles which tell us what the thread is about.
Pierre Bungle
Greenhorn

Joined: May 05, 2009
Posts: 9
Thank you very much for the help, much appreciated!

I'll use quotes next time
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
CODE in that case, not QUOTE, please
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: A little help for a total beginner?
 
Similar Threads
Array quiz I can't solve
Understanding Objects and Reference Variables
Head First Jave Exercises - Code in Class Calls Itself?
Someone teach me line by line how to read this particular code
A Heap O� Trouble :-- Head First Java