aspose file tools*
The moose likes Servlets and the fly likes Need HELP on Class.forName()....! ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Need HELP on Class.forName()....! ! " Watch "Need HELP on Class.forName()....! ! " New topic
Author

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

river down
Greenhorn

Joined: Dec 11, 2002
Posts: 21
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

Joined: Mar 22, 2000
Posts: 12683
    
    5
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


Java Resources at www.wbrogden.com
river down
Greenhorn

Joined: Dec 11, 2002
Posts: 21
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

Joined: Dec 11, 2002
Posts: 21
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
 
 
subject: Need HELP on Class.forName()....! !
 
Similar Threads
explain the code output
drop down list problemt - Pls Help
Example from Angelica Langer FAQ - need help
Sorting an ArrayList of Object X
writing jsp codes