File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how to create new class instance on the fly - do I need an inner class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to create new class instance on the fly - do I need an inner class? " Watch "how to create new class instance on the fly - do I need an inner class? " New topic
Author

how to create new class instance on the fly - do I need an inner class?

Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 137
Hi,

I would like to call this method for a range of numbers, and store the results in a class called Set4 (as the results are a set of 1 to 4 numbers).
variable count tells me how many instances will be required, but how do I create and use them within the loop, do I need an inner class?
What methods could one use to get around this efficiently? (I am not experienced with inner classes at this point.)

ERROR: (only one number sent from calling program at this point; value 100)

1: 5 5 5 5
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at numbers.Lagrange4sq.process4sq(Lagrange4sq.java:35)

line 35 is: set4s[count] = new Set4(); (line 29 below)

SET4 CLASS:




NB I cannot take credit for the logic in the nested loops, I give credit to the webpage below.



Many thanks,


Nigel
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

I'm not exactly clear what you're trying to do, but your problem comes on line 32, where you reference set4s[1]. The first time through that loop, count will be 1, and on line 28 you create set4s as an array with "count" members. Since Java array indexes are 0-based, the only valid reference is set4s[0]. In any other iteration, that would be OK since count would be greater than 1, but even then you would be creating an array and then only using one member of it. Then on line 40, you store the new object in the TreeMap, but always with the same key, which replaces what was there before. So not only do you have an array from which you only use one member, you have a TreeMap that only ever contains one object. In other words, something is wonky. Maybe the code you borrowed was bad to begin with, or maybe you corrupted it as you brought it over.

Also the problem noted at line 28 is not actually a problem. Variables declared inside a block only have scope within that block. That is, the next time you come through the loop, it's fine to declare the variable with the same name, since the old one will be out of scope already.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
You never need an inner class. You can't do anything with one that you wouldn't be able to do with a top-level class. Sometimes it is advisable to use an inner class, purely as a way of communicating your intent and keeping your code well-organized. When you have a discrete task that is logically separate from the rest of the enclosing class, yet relates specifically to it and is unlikely to be useful anywhere else, that's a candidate for an inner class.
Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 137
Thanks Greg, that sorted it.

Aside: The treemap entry of "OrderedResults.put(1, set4s[count-1]);" is ok, as there can only be one entry that meets those criteria and I am after a specific sort order with that one first.

Thanks for your help!

Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 137
Thanks Dennis,

Inner classes, I've read about them and will have to bite the bullet soon! I guess they become essential for GUI and polling for state changes, such as a file watcher service.

Many thanks,
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36497
    
  16
I have changed the red text to black and removed some space in the original post, because too much spacing and red text can be difficult to read.
Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 137
thanks Campbell, I'll try and format better in future
Nigel
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36497
    
  16
Are you really using a Set? If you add the same number several times, you only get one element.
Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 137
thank you Campbell, an oversight!
Nigel
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36497
    
  16
You’re welcome
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

Nigel Shrin wrote:Thanks Greg, that sorted it.

Aside: The treemap entry of "OrderedResults.put(1, set4s[count-1]);" is ok, as there can only be one entry that meets those criteria and I am after a specific sort order with that one first.

Thanks for your help!



Good, I'm glad you got that working! I guess it's good anyway. You still seem to be ignoring the fact that you are creating an array, then only using one member of it. Also, I don't understand your being "ok" with replacing the single element in your tree map with every iteration. Do you realize that's what you're doing? Sort order and first don't really come into it as a one element set is always sorted, and its single element is always first.
Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 137
Hi Greg, I've rewrittten that whole section now!
I was going to change the "1" key to a variable after adding the first element, but it's all changed now.

thanks for your help
Nigel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to create new class instance on the fly - do I need an inner class?
 
Similar Threads
need help with compareTo()
static method not running
Integer.ValueOf() Method
Help in writing a better program for Vampire numbers
How to detect circle in a directed graph?