Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Michael Sullivan
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Sullivan
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic