• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object Creation  RSS feed

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the go() method is not being called on the constructor. consider the call as (new SimpleChatClientA()).go().

The (new SimpleChatClientA()) returns an object and the go() method is being called on it. There is no need for storing the object in an instance as it is not being used anywhere else.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'.
so ..
SimpleChatClientA ref=new SimpleChatClientA();
ref.go() is same as ...
new SimpleChatClientA().go();


 
Chuck Mondi
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the reference variable name given to this instance when using new SimpleChatClientA().go();? Like how does the application know what object to call the method on?
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chuck,

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.

John.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't got a reference name.

Let us imagine we have a pointer to a Foo object, let's call that * (like Gary Trudeau drawing the President )
So what you have now is

*

The easiest way to get a * is to use new Foo().

You can assign that * to a reference

Foo fffooo
fffooo = *;

But you can't actually write * so you write

fffooo = new Foo();

As an alternative, you can call methods on * like this

*.execute();

But you can't write * so you can replace it with new Foo() like this

new Foo().execute();

I hope that helps a bit
 
Chuck Mondi
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ritchie/Everyone else,
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?
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!