• Post Reply Bookmark Topic Watch Topic
  • New Topic

cast method in class  RSS feed

 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a method called cast in Class Class which return an Object;

Am able to see that in my eclipse ide but not in the

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html

Could some one clarify !

If some one has used , please let me see your code snippet !
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look here you will find it. Note it says "since 1.5," so it isn't in Java 1.4.
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
If you look here you will find it. Note it says "since 1.5," so it isn't in Java 1.4.


Hey Campbell ..


That was a cool introduction - landing to java 1.5.

I never have visited java 1.5

You are my instantiator and loader for Java 1.5 !



Oh My eclipse is 1.5 compatible !

 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java 1.4 it won't make any sense to even have the cast method.

The trick about it, is that given a Class<T>, it returns T - or throws a ClassCastException. It's usefull if you need a T but are given something else, like when adding something to a generic collection. Class.isInstance should always go first of course

Now in Java 1.4, it won't make any sense since Class is not generic, and therefore cast could only return Object. You would have to cast it manually again.
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
In Java 1.4 it won't make any sense to even have the cast method.

The trick about it, is that given a Class<T>, it returns T - or throws a ClassCastException. It's usefull if you need a T but are given something else, like when adding something to a generic collection. Class.isInstance should always go first of course

Now in Java 1.4, it won't make any sense since Class is not generic, and therefore cast could only return Object. You would have to cast it manually again.



Thanks Rob,

Better will stic with java 1.4 then, for this case !

Will see them when i tend to encounter such generic template classes !

Any how that was a great day for introducing me in to java 1.5 !

Thanks Campbell !


 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

I don't understand your wish to stick with Java 1.4 however. Java 5 came out almost 4 years ago and you ought to be learning its enhancements.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. I've been quite hesitant when moving away from Java 1.4, but once I did so I was quite glad I did. Java 6 (which was already available - I totally skipped 5.0!) has so much extra benefits, generics being just one.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!