• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to type cast the Object if im having class Name as string?

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All



How to make the above code to work.
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are you expecting this code to do? If you want to cast the first element in the list to String, why don't you just write it?

EDIT: ahh think I understand now what you want. As far as I know, there is no such replacement mechanism as you intend. But perhaps you can use something like


 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey D. Ogranos you said correct…

Santhosh your not much clear, I hope, you are expecting something like this I guess.

This may helpful for you Santhosh gowda


[javadoc]
List _List = new ArrayList();
_List.add("san");
_List.add("10");
String _String = (String)typeCastAs("java.lang.String", _List, 0);
Integer _Integer = (Integer)typeCastAs("java.lang.Integer", _List, 1);

public typeCastAs(String type, List list, int index){
Class _Class = null;
Object result = null;
try{
_Class = Cass.forName(_String);
result = _Class.cast(_List.get(index));
}
catch(ClassNotFoundException ignore){
;
}
return result;
}
[/javadoc]

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch Mohanasundar Ranjani

Please you CODE tag to post your code . so that it can be easy to read.
 
Mohanasundar Nagarajan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohanasundar Ranjani wrote:Hey D. Ogranos you said correct…

Santhosh your not much clear, I hope, you are expecting something like this I guess.

This may helpful for you Santhosh gowda




 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

D. Ogranos wrote:


What's wrong with instanceof?
 
Mohanasundar Nagarajan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
instanceof is correct nothing wrong.
Think I can add n number of instance to list every thing I cannot tell it in if eles....
thats why method said me is ok
Santhosh has to decide which is better because he asked doubt

Rob Prime wrote:

D. Ogranos wrote:


What's wrong with instanceof?

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Mohana : what is the significant/advantage of D. Ogranos's approach? Rob approach is simply. is it n't ? but generic can be even better
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:What's wrong with instanceof?



Nothing really. I think what santhosh wanted was to provide a class (with a given name, String in the example) and have the object(s) in the list casted to that class. So my thought was to find the actual Class object with the given name, and then to cast to that class. Class has a cast() method, but it doesn't work as I expected:



So the cast() method is kind of pointless if I understand it right, and you really have to use an if-else construct and test which class you have...and then you can just use instanceof right away, as you suggested.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic