• 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

is it possible to generate classes dynamically

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

I wonder if it's possible in Java, to generate classes dynamically. In my case, there should be an xml file with entries such as

<class>com.test.Car</class>
<class>com.test.Boat</class>

to be easily updatable. The code then should iterate through the list and add
List<Transportation> transportation = ArrayList<Transportation>();
transportation.add("....".class);

Can that be implemented?
Thanks
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class.forName(...) can be used to get Class objects for which you have the fully qualified class name as a string. If -instead of a Class object- you want an instance of that class, you can call Class.forName(...).newInstance(), assuming that class has a no-argument constructor.
 
Denis Wen
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I think that's what I was looking for.
 
Denis Wen
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more question.


Here is the snippet

Class clazz = Class.forName(classStr);

As far as I understand I am getting an object of type Class through calling Class.forName(). What does this object represent?
Is it the same as when calling someObject.class?

Eclipse underlines the Class saying "Class is a raw type. References to generic type Class<T> should be parameterized." How is it that a type Class is raw?

Using information from calling Class.forName, is it possible to instantiate the following object.

SomeNiftyObject<T>("".class);

How cann I pass a type <T>?

Many thanks
With

new SomeNiftyObject<T>(.class)
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here is the snippet

Class clazz = Class.forName(classStr);

As far as I understand I am getting an object of type Class through calling Class.forName(). What does this object represent?
Is it the same as when calling someObject.class?



Yes, except the someObject class is known at compile time. With the forname() method, you are using a string to load the class -- which may not be known until runtime.

Eclipse underlines the Class saying "Class is a raw type. References to generic type Class<T> should be parameterized." How is it that a type Class is raw?

Using information from calling Class.forName, is it possible to instantiate the following object.

SomeNiftyObject<T>("".class);

How cann I pass a type <T>?



The Class class uses generics. So, eclipse is asking you to specify what it is ... something like Class<SomeClass>. However, in this case, you don't know the class type at compile time. Just ignore the warning.

Henry
 
Denis Wen
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry,

is it possible to infer Type of class obtained through Class.forName. I need it to instantiate a generics object then?

new SimpleDAO<T>();

thanks
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Denis Wen:

is it possible to infer Type of class obtained through Class.forName. I need it to instantiate a generics object then?

new SimpleDAO<T>();



Class.forName() is class loading at runtime. Generics is at compile time, all types are erased by the time the forName() method is used.

But... if you can infer the type, at compile time, then you don't need to use the forName() method, you can simply use SimpleDAO.class.

Henry
 
Denis Wen
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks!
 
Sheriff
Posts: 22783
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

Originally posted by Henry Wong:
However, in this case, you don't know the class type at compile time. Just ignore the warning.


I disagree - use Class<?> instead.
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:

I disagree - use Class<?> instead.


As a further addition to Rob, read Chapter 5 of the excellent "Effective Java 2nd edition" on why Class<?> is a good idea in this case. Here is a link to that.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic