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
posted 7 years ago
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..
You'll never achieve your goals if you don't take that chance, so go pry open that trunk and get those amps.
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..