File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Creating References to Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating References to Objects" Watch "Creating References to Objects" New topic

Creating References to Objects

Landon Blake
Ranch Hand

Joined: Oct 15, 2004
Posts: 44
I had a couple quick questions about how to create references to objects.

1. In the first scenario, I want to create a reference to an object of the type NewObject. I want to set this reference equal to the return value of the createNewObject method of the newObjectFactory object. Would my code look like this?:

NewObject sampleNewObject = (NewObject) newObjectFactory.createNewObject();

(P.S. - The createNewObject method is defined to return a NewObject object.)

2. I have a collection of implementInteface objects that implement an interface called TestInterface. When I return an object from this collection, how do I call a method of the TestInterface, since the collection returns a generic object? I think there is some type of casting involved, but I'm not sure what the format of the code would look like. Can anyone help me with this?


[ October 15, 2004: Message edited by: Landon Blake ]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Hi Landon,

Welcome to JavaRanch!

As to your question #1, you can just write

NewObject sampleNewObject = newObjectFactory.createNewObject();

I took out the "(NewObject)" which isn't needed here, as you say that createNewObject() already is declared to return type NewObject. That construct is called a "cast", and what it does is tell the compiler to treat a reference of one type as if it were a reference to some other type. Here, the types match.

As to #2, I can't say I understand the details exactly, but you may simply need to do something like

TestInterface ti = (TestInterface) collection.get(0);

This gets the first item from this collection, and tells the compiler it's OK to treat the raw Object as a TestInterface.

[Jess in Action][AskingGoodQuestions]
Landon Blake
Ranch Hand

Joined: Oct 15, 2004
Posts: 44
Thanks for the response Ernest.

You answered both of my questions. I was confused about the appropriate use of that argument in parentheses on the right side of the equal sign. (What is the proper name for it? - a "construct"?) But I think I've got it now. You only need to use that argument when you are casting to a type other than the one called by the "constructor" method. Is this correct?

For Example:

I have a class named Monster, and an object named monsterFactory that implemements a createScaryMonster() method that is used to return Monster Objects. I also have 2 subclasses or subtypes of Monster, (1)Cyclops and (2)Godzilla.

If I want to create a reference to a Godzilla Monster object named greenGodzilla I would use:

Monster greenGodzilla = (Godzilla) monsterFactory.createScaryMonster();

If instead I want a reference to a Cyclops Monster object name oneEye I would use:

Monster oneEye = (Cyclops) monsterFactory.createScaryMonster();

Finally, if all I wanted was a reference to a generic monster object named
anyOldMonster I could simply use:

Monster anyOldMonster = monsterFactory.createScaryMonster();

Are all the above scenarios correct?

Thanks again for the assistance,

[ October 15, 2004: Message edited by: Landon Blake ]
marc weber

Joined: Aug 31, 2004
Posts: 11343


The parenthetic preceding the object on the right of the equals sign is called an "explicit cast," because you're explicitly "casting" the type of the object. Let me try to explain when this is needed...

The createScaryMonster() method is returning an object of type Monster. If you're assigning this to a variable declared as type Monster, then no cast is needed because you're handing the variable exactly what it expects. (Or more accurately, what the compiler expects for that variable.)

Monster someMonster = monsterFactory.createScaryMonster();

In fact, if you're assigning the Monster object to a variable declared as a parent class of Monster, then you still don't need an explicit cast; because upcasting (from subclass to parent) is "safe" and can be achieved automatically through "assignment conversion."

For example, suppose you had a parent class called Creature, with Monster extending Creature. Now, if your variable is expecting a Creature, and you hand it a Monster, it's still okay without an explicit cast because every Monster is a Creature.

Creature bigCreature = monsterFactory.createScaryMonster();

Note that although there is no explicit cast here, the Monster being assigned to the variable is being implicitly upcast (automatically converted) to the parent type Creature.

So when do you need an explicit cast? When you're going the other way -- when you're downcasting. If your variable is expecting a Godzilla, and you hand it a Monster, then there might be a problem because not all Monsters are Godzillas. So here you need to assure the compiler that the Monster you're assigning is, in fact, an instance of Godzilla.

Godzilla bigG = (Godzilla)monsterFactory.createScaryMonster();

Now, the explicit cast to type Godzilla will satisfy the compiler. (However, if you're wrong, and the Monster you're passing actually turns out to be a Cyclops, then you'll get a RuntimeException.)

I hope this makes sense... :roll:
[ October 15, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Landon Blake
Ranch Hand

Joined: Oct 15, 2004
Posts: 44

This make perfect sense. Thanks for the clarification, and for using my example. You guys do a great job!!

I agree. Here's the link:
subject: Creating References to Objects
It's not a secret anymore!