File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

newInstance()

 
Arun raghvan
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the use of newInstance when i can create object of the class using new operator.
 
Tom Reilly
Rancher
Posts: 618
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lookup the singleton design pattern. An extension of the singleton is caching and reusing objects.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47232
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As i know it's used to call default constructor.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 272
C++ Debian VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
K Abhijit
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic