Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can you explain what these parts of code mean?

 
andrew dale
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm reading "Head First Java" STUCK in chapter 2, it basically assumes that you already know some code, which I don't but, I learn quick....

Starts talking about objects/class - and shows basically how it works but does not talk about code and all the sudden just gives me this:




SO CONFUSING........ whats up with the string breed and name? It's not doing anything it looks like....

d.bark() <--- ??? how does that know what to do???

I basically understand nothing of this whole code...

Can anyone help break it down for me? I've been through the chapter twice and taking notes trying to figure this out.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12202
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, first, that can't be the code they give you in the book. There are at least 4 errors that need to be fixed before it will compile. It can be EXTREMELY hard to help you if you don't give us accurate information, such as what the code really looks like.

Having said that...

When you define a class, you are creating essentially a blueprint. You know there will be a bedroom, but you don't know what color the walls will be until you actually build the house - and maybe not even then. The house may be up for a while before someone decides what color to paint the walls.

So, someone here has said "Any Dog will have three things - a size, a breed, and a name." In the code provided, nobody bothers to tell us what those last two are, but we have a space to record it once we find out.

Further, the Dog class defines what a Dog can do. In this case, all a dog can do is bark.

In the class DogTestDrive, we actually create a Dog object (that 'new Dog()' line). we will refer to this dog with a reference called 'd'. I could create a second dog with a line like

Dog e = new Dog();

but in any case, java knows that 'd' is a Dog object. So when we say "d.size = 40", we record for THAT SPECIFIC DOG it's size. This would have no effect on the size of my dog 'e'. Just like if I built two houses from the same blueprint and painted one bedroom orange, that would have no effect on the color of the same bedroom in the other house.

Java also knows that for Dog objects, there is a method called "bark" that takes no arguments. So, when you say "d.bark();", you are saying "run the code associated with the Dog.bark() method", which in this case, should print "Ruff Ruff!".

 
andrew dale
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow that actually helped A LOT !!! Thank you!

Sorry if I misled you - here is the exact page out of the book.

I tried to copy it into here but the errors probably came from me..


 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred has given you a very clear explanation, but I would like to add some too. First, the Dog code is incomplete in this chapter. This code is just a sample to teach you about objects, that is why they have left two strings along. The completed coded will be available on Chapter 4, page number 76.

I am not gonna explain what is happening here because previous poster has already done that very well, but I can see a MAIN error in you MAIN method. This is your main method



it is wrong. It should be
 
fred rosenberger
lowercase baba
Bartender
Posts: 12202
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yohan Weerasinghe wrote:but I can see a MAIN error in you MAIN method. This is your main method



it is wrong. It should be


that's one of the errors I was referring to. You'll also notice that there is a lack of several closing braces to end the various methods/classes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic