Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2005

Tip: Looking for answers? Try searching our database.

instantiate class from string

Thread view: 
sarath - 29 Apr 2005 06:47 GMT
1.

I have a method,

public void something(String fullyQualifiedClassName)
{
}

the parameter passed to the method is a fully qualifed class name for a
class. I have to instantiate that very same class inside the method.
how can u do it ?

2.
Why string is immutable which is an additional overhead ?

thanks in advance

sarath
Peter MacMillan - 29 Apr 2005 07:39 GMT
> 1.
>
[quoted text clipped - 7 lines]
> class. I have to instantiate that very same class inside the method.
> how can u do it ?

Here is an example that should get you started:

//--- CreateObject.java
public class CreateObject {

    public static class SomeClass {
        public void sayHello() {
            System.out.println("Hello!");
        }
    }

    public static Object createObject(String className)
            throws ClassNotFoundException, InstantiationException,
            IllegalAccessException {
        return Class.forName(className).newInstance();
    }

    public static void main(String[] args) {
        try {
            Object o = createObject("CreateObject$SomeClass");
            if (o instanceof SomeClass) {
                ((SomeClass) o).sayHello();
            }
        } catch (Exception e) {
            System.out.println("Error: " + e.toString());
        } // nb. you'll really want to catch the exceptions.
    }
}
//---

> 2.
> Why string is immutable which is an additional overhead ?

There are a number of reasons why java.lang.String is immutable. Thread
safety is sometimes important. I think the API docs do a so-so job of
explaining why you might want an immutable String:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

> thanks in advance
>
> sarath

Signature

Peter MacMillan
e-mail/msn: peter@writeopen.com

raghupulikkal@gmail.com - 29 Apr 2005 08:48 GMT
Thanks Peter.
That was one good explanation..

sarath


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.