aspose file tools*
The moose likes Beginning Java and the fly likes Can you explain what these parts of code mean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can you explain what these parts of code mean?" Watch "Can you explain what these parts of code mean?" New topic
Author

Can you explain what these parts of code mean?

andrew dale
Ranch Hand

Joined: Aug 15, 2011
Posts: 45
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.


You'll never achieve your goals if you don't take that chance, so go pry open that trunk and get those amps.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

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!".


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
andrew dale
Ranch Hand

Joined: Aug 15, 2011
Posts: 45
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

Joined: Oct 07, 2010
Posts: 497

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


Are you better than me? Then please show me my mistakes..
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can you explain what these parts of code mean?
 
Similar Threads
Moving from A of beginning to B of beginning
cannot find symbol
New to Java
Program from "Head First Java" won't run
Learning about objects