Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need HELP on Class.forName()....! !

 
river down
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I have a problem in Class.forName() actually it was related in Collection.sort() as well...see the code below

Collections.sort(flightList, new(<class-name>
As u can see the flightList is a vector containing my data and the 2nd argument w/c is an argument class have to derived from a variable...
How will I do it....can I used a class that forName<variable> where variable is the class name..
I code the followiwng
Collections.sort(flightList,(SortClass)(Class.forName(sortType).newInstance()));
where variable sortType is equal to a class name "SortDATACLASS"...
But it didnt work but when u execute the following it works directly..
Collection.sort(flightList,new SortDATAClASS())
but it must not be hardcoded(class name) it must come into a variable that changes everytime....
Can someone help pls....
Thanks
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error or exception did you get? Saying "it didn't work" does not give us much to go on.
If your sortType does not include a complete package.classname that could be the source of your problem.
Also - why did you stick this in the servlet section???
Bill
 
river down
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try to include the package.....I stick to the servlet cause it was how the framework that clients used...although we argue about it....but client are demanding...anyway....i'll try...any other solutions..if it did'nt work....do I really have to cast to a class when using Class.forName()...as u can see the SortClass that I used in the above codes only implements the comparator and no implementation of the compare() method....instead its implementation lies in the value of the variable (sortType...holds the name of the class)....?
Thanks very much in advance
 
river down
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its working I just include the package and do some changes in the comparator class....both the CASTing class and the actual comparator class....thanks a lot
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic