aspose file tools*
The moose likes Java in General and the fly likes Object o = (Object)new Foo().... explanation pls Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Object o = (Object)new Foo().... explanation pls" Watch "Object o = (Object)new Foo().... explanation pls" New topic
Author

Object o = (Object)new Foo().... explanation pls

Michael Sullivan
Ranch Hand

Joined: Dec 26, 2003
Posts: 235
lets assume that this is some of my code:
1 class Foo{
2 public int i = 3;
3 }
4 Object o = (Object)new Foo();
5 Foo foo=(Foo)o;

this is typical of what I am seeing in some sample texts. I've numbered the code so that I might discuss it easier. Line 1 starts the class Foo. 2 declares and initializes i. Can someone please give me the simple explanation of whats going on in lines 4 and 5? This just looks like a different kind of object instatiation than I'm used to, especially with Object inside of parens. Line 5 is lost on me.
Thanks in advance!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Howdy Doc,
Welcome to JavaRanch!
We don't have many rules round these parts, but we do have our naming policy which requires that your display name include both a first and last name and not sound fake. Note that most folks use their real name -- keeps us honest. Please head over here and fix yours, pronto. Thanks, pardner!
As to your question: the name of a type inside parentheses is called a cast. It's an instruction to the compiler that the thing following the cast should be treated as if its type were the type given in parentheses. The cast on line 4 is actually unnecessary, brcause
Object o = new Foo();
is perfectly legal, since Foo extends Object. The cast on line 5 tells the compiler that, although the type of "o" is "Object", you happen to know that "o" is really pointing to a Foo object, and so it's OK to assign "o" to "f".
Casts are checked at runtime, and if it turned out you lied to the compiler, you'll get a ClassCastException.


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
It may also be worth noting that the code shown is pretty silly, probably only written this way to show examples of casting. Lines 4 & 5 could be replaced quite simply by

There was no apparent need to cast to Object and back, other that to show examples of casting.
Looking at line 4 by itself (without replacing 4&5 as suggested above), there is still no need to cast to Object at all.

This would also work. Casting to Object is unnecessary because Object is a superclass of the declared tyle of f; a subtype can always be converted to a supertype without an explicit cast. A Foo already is an Object; the compiler requires no special convincing of this.


"I'm not back." - Bill Harding, Twister
Michael Sullivan
Ranch Hand

Joined: Dec 26, 2003
Posts: 235
Thank you both for your replies, and I've changed the name to conform to the rules/ regs.
Now, back to that code.... it looked silly to me at first glance, because I'm used to seeing
Foo foo = new Foo();
so when I saw the casting in the constructor, I thought to head over here (pronto, as it were). Though the code looks silly and redundant, I wanted to see what the real world use of it is... and now I know, to confuse me.
You see, I've found code written like this in texts and online material that I'm using to study for the SCJP test. Your probably wondering why I didn't post in that forum, and I'll tell you: though I'm interesting in passing the test, I need to understand the concepts first (even if the sample questions are obtuse).
[ December 26, 2003: Message edited by: Michael Sullivan ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
though I'm interesting in passing the test, I need to understand the concepts first (even if the sample questions are obtuse).
Excellent attitude. Good luck.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object o = (Object)new Foo().... explanation pls