• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to Instantiate an Object of a class having dynamic name

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone
I want to create an object of a class dynamically in a for loop...eg:
I have 3 classes...MyClass1, MyClass2, MyClass3....Now, i want to create an object of it using for loop ..eg :
for(int i = 1; i < 4 ; i++){
MyClass+i obj+i = new MyClass+i();
}
i Know my code is completely wrong .... but not able to get the solution....Reflection concept also not giving the solution ...
Thanks in Advance!!!
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reflection is the way to do this - in particular you'd want to look at the methods in the java.lang.Class class.

But reflection isn't really a beginners topic, and it's entirely possible that the best solution to what you want doesn't involve dynamic class names at all. If you explain why you're trying to do this we may be able to suggest a better approach.
 
Tarun Oohri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:Reflection is the way to do this - in particular you'd want to look at the methods in the java.lang.Class class.

But reflection isn't really a beginners topic, and it's entirely possible that the best solution to what you want doesn't involve dynamic class names at all. If you explain why you're trying to do this we may be able to suggest a better approach.



Thank you Sir for replying . Actually i have 3 classes, ExtractPDF_1,ExtractPDF_2,ExtractPDF_3, each of which extract data from the uploaded PDF...Now if a user upload a PDF,
by default it goes for extraction to ExtractPDF_1...Now if this file is not able to extract fully ...it should go to other file and so on ... for this i want instantiate in for loop...Hope i am able to clarify what i want to do..
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tarun Oohri wrote:
Thank you Sir for replying . Actually i have 3 classes, ExtractPDF_1,ExtractPDF_2,ExtractPDF_3, each of which extract data from the uploaded PDF...Now if a user upload a PDF,
by default it goes for extraction to ExtractPDF_1...Now if this file is not able to extract fully ...it should go to other file and so on ... for this i want instantiate in for loop...Hope i am able to clarify what i want to do..



So you do know the three class types, why do you need to code it dynamically? It is not much hardship to just create the three types as three different variable declarations. And it is definitely much easier than using the reflection classes !!

Henry
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tarun Oohri wrote: Actually i have 3 classes, ExtractPDF_1,ExtractPDF_2,ExtractPDF_3, each of which extract data from the uploaded PDF...


If those are the actual names you are using, they are horrible. Names should help the reader get an idea of the intent but these names don't do that. Your names should include something that helps explain what differentiates the functionality of one extraction method from the other extraction methods. No offense but frankly, using only 1, 2, etc.to make a distinction is misguided at best and just sloppy, lazy programming at worst. Sorry if that sounds a bit harsh; I'm just sayin'.

I agree with Henry, you don't have to do this. I suggest you look at the chain of responsibility pattern, this will be a better way of organizing your logic, IMO.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote: . . . reflection isn't really a beginners topic . . .

Agree. Moving discussion.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tarun, I think you should be doing something completely different than what you are trying to do.

It is not possible in Java to create variables with dynamic names.

You should probably use an array, or a collection class such as a List or a Map to store the objects you are creating.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic