Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class.forName(ClassName)

 
Muni K Reddy
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Guys,

I would like to make my own Custom class and call it using Class.forname() method fromt he Reflection API. Could you please give me a few steps in the right direction to help me achieve this?

Does the class being called dynamically have some special properties. Is the parameter inside Class.forname(parameter) a fully qualified path of the class?

Are there any disavantages of calling a class dynamically?

Im trying the following:

And I am trying to run the method setX() by calling the class TestClass dynamically i.e without making an instance of TestClass using the "new" keyword.


Many Thanks

[ September 17, 2007: Message edited by: Muni K Reddy ]
[ September 17, 2007: Message edited by: Muni K Reddy ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't "call" a class. You "load" a class. You "call" a method within the class.

The parameter of Class.forName() is the fully-qualified (i.e. including the package) class name.

There are many disadvantages of using dynamic class loading and reflective method calling. The code has far fewer compile-time checks, so bugs can more easily get into the running code. The code is much more verbose and harder to read. One would never use it unless it is actually necessary (or if one is just doing it as an exercise).

Dynamic class loading and reflective calling of methods can do things that are simply not possible any other way. For instance, loading a class whose name is defined in a configuration file, whose contents are only known at run-time. For this reason, they are great features, but not to be used at random.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the parameter inside Class.forname(parameter) a fully qualified path of the class?
Yes, always.

Are there any disavantages of calling a class dynamically?
Things have improved dramatically since Java 1.2, where reflection took about 200 times longer, and while it won't be this bad the code will still be slower, even if you cache references (methods, constructors, fields etc) after discovering them.
 
Muni K Reddy
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for you replies! I now got myself confused a bit by reading the the following in an article:

Can you avoid changing the code you have already written and tested to add new functionality? Can you add new classes to your program without having to recompile the whole thing? The answer to both questions is yes, and as you might have guessed, is based on interfaces and dynamic class loading.


If there is a need to add functionality to your own classes, why would you be using dynamic loading? I will always know all the methods in it so why not use the "new" keyword and do a static loading to load the classes and add functionality??

Here's the article!Im still trying to understand the example in the article and would like to know if the given dynamic loading example is the correct design pattern for such scenarios!

Would be glad if someone could help me clarify my confusion!
[ September 17, 2007: Message edited by: Muni K Reddy ]
 
todd runstein
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muni,

There are only a few patterns where doing dynamic loading would be your first choice. If you know what class you want to use, call one of it's constructors with "new".

However, if you don't know what class you're going to be using, you could do dynamic loading. For example, maybe you have a "RuleSet" interface. Perhaps you have several classes that implement this and you routinely add more. Perhaps you want to store information in a database (maybe about a Procedure, ManufacturingStep, etc) - you can store the fully qualified classname of the RuleSet that belongs to this record and load the class dynamically using this value. There are, of course, other ways to solve this problem, and other problems that lend themselves to dynamic class loading.

Hopefully that gives you a better idea of where and when you'd use dynamic loading. Assuming you're going through this for learning, here's an idea of how you could use dynamic classloading to call an "apply" method (totally off the top of my head - untested):

-- File rule/set/RuleSet.java


-- File rule/set/BasicRuleSet.java


-- In some other class


Hope that helps!
 
Muni K Reddy
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Todd,
Thanks a lot. Just loaded a class dynamically!
That was a beautiful explanation! Owe you a beer!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use things like this all the time ... using RuleSet from the last examples ...

This code can be "closed" to future change, but "open" for extension by adding new RuleSet implementations. We can put new key-ruleClass pairs in configuration and load them without touching this code.
 
Muni K Reddy
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stan!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic