• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A little help for a total beginner?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).

 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for the help, much appreciated!

I'll use quotes next time
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
CODE in that case, not QUOTE, please
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic