This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes class typecast question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "class typecast question" Watch "class typecast question" New topic

class typecast question

Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Hey yall, I'm trying to typecast an object from a class and I get the following error when I try to run it:
at funnelexppkg.funnel.tryMove(
the code in question is:

while this looks like a valid cast, it will not compile.
Any ideas?
Thanks in advance
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
As you can see on line 89, you define temp to be an Object:
Object temp = new Object();
However, on line 90, you attempt to cast it to a "marble". That of course freaks out, since it is an Object -- not a marble.

Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Well, that works, but now I am getting an underFlowException on this line:

[ March 17, 2004: Message edited by: Gabriel White ]
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
still need some help....
I really don't see why I can't perform a statement like this one:
This is a valid class cast but I still get a hang up on it.
as you can see temp is an object but I need to cast it as a marble object, as the method requires me to return a marble object.
I just am having problems doing this.
...I figured out the UnderflowExceptions
any help is appreciated.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
You cannot change the type of an object.
In Java, casting is more a mechanism of using a reference of some type, to get a reference of a different type that points to the same object.
When you created your temp object, the object (and the reference) were of type Object. An Object is not a marble (though a marble is an Object), so you cannot have a reference of type marble that points to an object that is only of type Object. The marble type reference would only be good for pointing to objects of type marble (or one of its subtypes).
Take a look at a few of the JavaRanch Campfire Stories, especially the "How my Dog learned Polymorphism" story. The "Cup Size" and "Pass-by-Value Please" stories are good reads as well.
Are things making a bit more sense now?

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Im just saying that there is nothting wrong with this:
Marble is a class. I have casted a temp object as a marble object. However my compiler will not let me get by it. It keeps giving me a classcast exception.

Thanks Dirk
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
Originally posted by Gabriel White:
Im just saying that there is nothting wrong with this:

Thanks Dirk

You need to look at the dequeue method of your ListQueue class. It is that return type that is incompatible with Marble.

The nice thing about Standards is that there are so many to choose from!
I agree. Here's the link:
subject: class typecast question
It's not a secret anymore!