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
> 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