Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting the arguments used in a written object's constructor  RSS feed

 
Brendan Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I'm using reflection to create instances of objects that have been written to file. I need to read what arguments have been passed into the written obect's constructor. This is what I've been doing when I know what arguments to pass into the constructor...



But now that I don't know what arguments have been passed, I can't properly instantiate the object.

 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah... you mean you don't know which constructor to use? Your question as we have it so far is the same as "I have a class which might have any number of constructors, how do I know which one to choose?" Notice that we out here know nothing about the class you're trying to create an object of.

So, you're going to have to write code which gets that list of classes from somewhere else in your program. In other words you're going to have to modify the design of your program so that information is passed to this code.

By the way line 1 in your second example can be simplified to this:

 
Brendan Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's one of two constructors. But before the object was written to file, it had already been instantiated and the code was ready to be executed. The program then went through a shutdown and the object was saved so it can be used when the program is restored. Now I want to know what exact arguments were passed to this object before it was saved, so I can instantiate it the same was as before. Hope this makes sense.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!