• Post Reply Bookmark Topic Watch Topic
  • New Topic

References / Classes / Methods (Provoked by Java First)  RSS feed

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

I am currently around the middle of chapter 5 of Java Head First Second edition. Chapter 4 (using methods to pass arguments and parameters) was a breeze for the most part, but a few things about chapter 3 (references) still have me scratching my head:



1) Why exactly do references ('remote controllers') exist? Why can't I just create a new object and access the new object directly? (Pg. 55)

2) Furthermore, why do they point out that two references can refer to the same object? Are they just being highly theoretical, because to me this seems a bit pointless? In other words, why would one require two remote controllers for the same TV? (Pg 57)

3) Why is it that some classes exist completely unrelated to main (which is what, exactly, a method, right? It has parantheses, after all...) and other classes actually CONTAIN main. Example A on page. 63 has the Books class out on its own, and then the BooksTestDrive class afterwards. The latter makes sense to me, the former does not.

4) And one question from chapter 4: Sometimes Head First creates a class, and then within that creation creates a reference variable and an object of that class. Despite being a bit confusing (finish creating the class first kthx?), this also has me a bit disoriented because in many cases a class's method will be invoked prior to the method even being coded. Should I take this to mean that the compiler reads through the entire .class so that a method can function even if I as the reader haven't seen it yet, reading from a top->down fashion? Example: en pg. 88, example A. The entire document consists of one class, which is invoked INSIDE itself, and its method is invoked shortly afterwards. Despite the fact that the method isn't even created until the bottom of the document.

Thanks so much!
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

1) Mostly because they're very, very convenient. If you have a reference called "currentPlayer" in your game, you're going to want to make that reference point at different Player objects as the game goes on. If you don't have references, you can't do that, and the Player objects end up getting copied around -- which makes it extremely hard to keep track of player 3's score, if at any one moment more than one object claims to represent player 3!

2) It's not only not pointless, it's common and important. To continue the game analogy, you might have variables that refer to player1, player2, player3, and player4, and another variable named currentPlayer that could point to the same object as any one of these at any given time.

3) Every method -- including main() -- has to be inside a class in Java. Some classes are in fact just there because there has to be someplace to put main() -- no other reason. Other classes are really used like classes -- i.e., you create instances of them, and send them around. In the C++ language, methods (or functions) can exist outside of a class, and so main() in that language is not part of a class.

4) Yes, the compiler "javac" turns the whole *.java file into a *.class file first; then that *.class file is the one that actually gets executed. So the compiler has, indeed, looked at the whole file before anything runs.
[ May 22, 2008: Message edited by: Ernest Friedman-Hill ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!