Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
This is code from the Head First Java book in chapter 15 when they begin talking about Sockets and multithreading. I have a question regarding line 59. First, where is the reference variable at? Why is there not a need to create a reference variable? Second, how are they calling the go() method on SimpleChatClientA() constructor? The go() method isn't static so I would expect it to only be called on an instance.
new SimpleChatClientA() : - this is to create an object (new keyword is important)
a method can be called using 'object' ... or by using the 'reference' variable
that holds (points) the 'object'.
SimpleChatClientA ref=new SimpleChatClientA();
ref.go() is same as ...
The JVM has an internal representation of every object that gets created during its lifetime. You can get an idea of what it's doing by calling System.out.println() on a class that doesn't have a toString() method defined. That's how it knows which object to call go() on.
Thanks for the info. I'm not familiar with pointers but I can research that. When should I use new Class(); versus Class myClass = new Class()? Should I only use the shortened version when I don't need to use a reference variable throughout my application?
Pointers are the location where something resides in memory. There is no means of direct access to such pointers in Java, but there is in other languages. Regard pointers as an advanced topic which you don't need to know about at this stage.
You can use the short version (new Foo().execute();) whenever you want to use an object once and once only. If you need access to the same object twice, you must give it a reference with an identifier (= name). By the way: you call that an anonymous object.
And you're welcome.
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss