Learning Java: Day 4

How Objects Behave
We are now in Chapter 4 of "Head First Java" and today we learn about object behavior and how state affects behavior and behavior affects state.  No, this is not a book on parenting or sociology, nor a discussion of Washington politics. This is about getting java objects to do what you want them to do. (Now if I could just get the resident teenager to consistently do what I tell her...)

Remember, state is what the class or object "knows" (aka "instance variable"), and behavior is what it "does" (aka "method"). 

First, though, I have to tell you that my genius programmer daughter-in-law explained how to get my program output to show up in the command window (command windows are such a hold-over from DOS days...) I've gone back to the beginning of the book to see if I just missed the instructions on how to get the java file compiled and then run.  Well, kind of.

The computer that I'm using has Windows7 installed on it.  For some unknown reason that is probably user error (that means I don't know what the heck I'm doing), I keep getting error messages when I try to compile my code using the "javac" command even though I'm SURE the path is set correctly.  In desperation, I finally associated the file extension "java" with the "javac.exe" program (control panel / Programs / Default Programs / Associate a file type or protocol with a program / select the .java extension / click the "Change Program" button / click the "Browse" button /; locate the "javac.exe" file (on my machine it is in Program Files\Java\jdk1.7.0_05\bin) / click the "Open" button. Voila, the .java extension is associated with the javac.exe program.

Now when I click on the filename.java file it automatically creates a class file.  THAT'S the file I needed to use to see my program results!  NOT the java file!  DUH!!!!  And I needed to use this format: "java (name of class file without .class extension)" which the book did NOT say, possibly because I was supposed to already know that. Whatever. The result is that I can now see my program output!  Yea! Hurray!  My DIL is brilliant!

"Jen says you're well encapsulated..."
Chapter 4 talks about encapsulation; that means hiding the stuff the class KNOWS (aka instance variables) by making it private so the data can't be changed accidentally (or maliciously) and making the stuff the class DOES (aka methods) public so that objects can access them. Encapsulation is used to help cut down on dumb mistakes like changing your number of items to less than zero, or completely losing the "phone number" (aka reference) to an object or other types of things that can crash a program and ruin your day. 

Remember the Make a Teddy Bear machine that we talked about in Chapter 2? The behaviors were getModel, setModel, getSize, setSize, getClothing, and setClothing.  You'd make the get and set methods public so other objects can access them but keep the state, that is the bearModel, bearSize, and bearClothing information private so some joker doesn't turn your Ballerina Bear into a Secret Service Prostitute without your say so. 

This chapter also discussed the importance of "creating" objects.  You can talk about (write code) objects and their behaviors all day but if you forget to new them (create an instance of an object; basically make an object), you're not going to have anything useful.
You're returning a what?
There's also discussion about how methods must declare a return type even if it doesn't return anything (you know how it is, you have to file a tax return even if you don't get anything back) and when something is returned it must return something compatible with the original type (kind of like expecting a Big Mac when at McDonald's and not a Chalupa.)

The rest of the chapter had the usual exercises and puzzles.  
I'm finding that I'm writing code that I don't completely understand and it is worrying me.  Will I understand more as I go along.  Will I understand everything in the book at the end?  Hoo boy...

0 comments:

Post a Comment