| Author |
newInstance()
|
Arun raghvan
Ranch Hand
Joined: Aug 22, 2010
Posts: 75
|
|
|
what is the use of newInstance when i can create object of the class using new operator.
|
 |
Tom Reilly
Rancher
Joined: Jun 01, 2010
Posts: 618
|
|
|
Lookup the singleton design pattern. An extension of the singleton is caching and reusing objects.
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
Tom I think that Arun is talking about the Class.newInstance() method.
The newInstance() method is part of the reflection concept. It allows you to analyze a class, its annotations, static variables, instance variables and invoke methods and even more.
If you're a java beginner I would advise you not to use this concept and focus on "normal" java.
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
Wouter Oet wrote: . . . If you're a java beginner I would advise you not to use this concept and focus on "normal" java.
If Wouter thinks this is beyond "beginning", then so do I. Moving thread.
|
 |
Arun raghvan
Ranch Hand
Joined: Aug 22, 2010
Posts: 75
|
|
|
As i know it's used to call default constructor.
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
|
Well it does calls the default constructor but not in a normal way. After a lot of security checks, it tries to find the default constructor using reflection and then tries to invoke that constructor also using reflection. As you can see it's not directly invoking the constructor. You also lose some compile time checks so unless you really really need it try to stay way from it. Most problems can be solved by good design and only if you're building a framework you should consider reflection.
|
 |
K Abhijit
Ranch Hand
Joined: Mar 03, 2008
Posts: 88
|
|
Sun defines Reflection as:
Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine
Suppose there is Worker class has method which has responsibility to create new Object of the parameter sent... something like
this might be called by several clients like
here WorkerClass doesn't know which Object is been passed as param to createObject at compile time
hence we can not have hook up with specific Contructor calll
So we use reflection--> newInstance() method to get job done...
Hope this helps
|
“The difference between 'involvement' and 'commitment' is like an eggs-and-ham breakfast: the chicken was 'involved' - the pig was 'committed'.”
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
|
That's not really true because reflection can't modify the workings of functions. For that you need a java byte code manipulator like javassist. And your example doesn't compile and fails when you pass an object of a class with no default constructor.
|
 |
K Abhijit
Ranch Hand
Joined: Mar 03, 2008
Posts: 88
|
|
@Oet:
this is working copy of code.
I posted only conceptual snippet... when copied it to Eclipse I could run the code after clearing few nominal exception like "Exception" clause and "static" modifier etc..
I don't see much difference between this copy and earlier that i'd posted... let me know incase i am missing anything here...
Cheers !!!
|
 |
Anand Hariharan
Rancher
Joined: Aug 22, 2006
Posts: 252
|
|
K Abhijit wrote:(...)
I don't see much difference between this copy and earlier that i'd posted... let me know incase i am missing anything here...
Here's what Wouter said:
Wouter Oet wrote:That's not really true because reflection can't modify the workings of functions. For that you need a java byte code manipulator like javassist. And your example doesn't compile and fails when you pass an object of a class with no default constructor.
(my emphasis)
Create a class C that has a constructor having one or more parameters (hence no default constructor) and use your WorkerClass.createObject to construct an instance of C.
|
"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
|
 |
K Abhijit
Ranch Hand
Joined: Mar 03, 2008
Posts: 88
|
|
thanks for the clarification Anand.....
Anand Hariharan wrote:Create a class C that has a constructor having one or more parameters (hence no default constructor) and use your WorkerClass.createObject to construct an instance of C.
I simply could not get this argument, the error would be simply because Class().newInstance() call default /(no argument) constructor...
This has nothing to do with Reflection: it's just normal concept.
if Class has at least one parameterized Constructor declared , we Can't invoke default constructor unless explicitly provided...
The illustration I gave here was for original question regarding usage /usability of "newInstance()" method ..So Kept It Simple and Stupid
Oet wrote:And your example doesn't compile and fails when you pass an object of a class with no default constructor.
Agreed but this is designing concern and needs to be considered while framing the Objects/systems...My example is very basic... with no other considerations
For that matter then every code would fail to compile at some point of time if we introduce some some things which as out of assumption or not supported by System.. say calling non supported constructor/method...
If my object needs parameterized constructor then I would make use of few other things in Reflection.
while designing do consider the scope and limitation keeping requirement in mind..
Suppose we have parameterized constructors, (don't have default), we would not able to make use Class.newInstance() as simple as that...
we've to do something like this
Output:
A::Inside Parameterized :: String xmlString
B::Inside Parameterized :: String xmlString
Object a is instance of class A
Object b is instance of class B
cheers
|
 |
 |
|
|
subject: newInstance()
|
|
|