new operator and class.forName

What is the difference between using new operator for instantiating a class and using the Class.forName() method..
[121 byte] By [sumeshzzza] at [2007-11-27 6:07:21]
# 1
class.forName() doesn't instantiate it. (iirc).
prob.not.sola at 2007-7-12 16:24:41 > top of Java-index,Java Essentials,Java Programming...
# 2
yes.. i think it returns Class object. But, is there any other way to instantiate a class other than using the new operator
sumeshzzza at 2007-7-12 16:24:41 > top of Java-index,Java Essentials,Java Programming...
# 3
> yes.. i think it returns Class object. But, is there> any other way to instantiate a class other than using> the new operatoryes there is.i think it's Class.newInstance() or something like that.
prob.not.sola at 2007-7-12 16:24:41 > top of Java-index,Java Essentials,Java Programming...
# 4

I am not familiar with class.forName but from my reading tonight it looks as though you would only use it in specific settings where you absolutely need it.

Check out [url=http://rds.yahoo.com/_ylt=A0geu7dN319Gh7AAM8tXNyoA;_ylu=X3oDMTB2ZHEzZ2FsBHNlYwNzcgRwb3MDMwRjb2xvA2UEdnRpZAMEbANXUzE-/SIG=12h1laial/EXP=1180774605/**http%3a//www.javageeks.com/Papers/ClassForName/ClassForName.pdf]this article[/url].

petes1234a at 2007-7-12 16:24:41 > top of Java-index,Java Essentials,Java Programming...
# 5

Class.newInstance() allows you to create an instance of a class dynamically, i.e. you don't know the type in advance.

However there are also patterns for that (such as Prototype and Factory), so it's not needed so often.

One case where class.newInstance() is useful is in plug-in type situations where you cast the class to an interface it implements, you know the interface at compile time, but the actuall class type is unknown.

-Kayaman-a at 2007-7-12 16:24:41 > top of Java-index,Java Essentials,Java Programming...
# 6

> I am not familiar with class.forName but from my

> reading tonight it looks as though you would only use

> it in specific settings where you absolutely need

> it.

>

Class.forName() causes the classloader to load the class in preparation for instantiating it, nothing more nothing less.

Can be useful in some scenarios, but not all that often.

jwentinga at 2007-7-12 16:24:41 > top of Java-index,Java Essentials,Java Programming...
# 7

> Class.forName() causes the classloader to load the class in preparation for instantiating it

Yes and this causes the static initialisation code in the class to run.

Likeclass LoadThisClass

{

private static string text = "hi";

static {

System.out.println(text);

}

}

tom_jansena at 2007-7-12 16:24:41 > top of Java-index,Java Essentials,Java Programming...
# 8
> yes.. i think it returns Class object. But, is there> any other way to instantiate a class other than using> the new operatorOthers- JNI, similar but not exactly new- Serialization which by passes the normal process
jschella at 2007-7-12 16:24:41 > top of Java-index,Java Essentials,Java Programming...
# 9

> > yes.. i think it returns Class object. But, is

> there

> > any other way to instantiate a class other than

> using

> > the new operator

>

> Others

> - JNI, similar but not exactly new

> - Serialization which by passes the normal process

Cloning

Class.newInstance has been mentioned, but Constructor.newInstance has not

georgemca at 2007-7-12 16:24:41 > top of Java-index,Java Essentials,Java Programming...