I'm in my first year of learning Java. Right now, we're only working with static methods to interpret data submitted in console. I'm trying to organize what I've got so far.
In my spare time, I've coded methods to handle exceptions with recursion when reading Byte, Short, Int, Long, Float and Double values, as well as other methods to pose further conditions on those values (having a minimum/maximum value, the input has to be withing an array of values...).
I'm trying to organize everything by function rather than data type, and I've recently found out about nested classes. Right now, my outer "Lire" (French for "Read", pastebin at the end) class has 11 nested classes (that handle numbers, numbers with minimal value, with maximum value...), each of those nested classes have 6 methods (sharing the same name as the methods in the other nested classes).
The way it's organized in a package, the command I'd use to call the method would be (translated) console.Read.NumberMinimum.readS hort(100) to read a number of data type Short with the minimal value of 100.
But, there's also console.Read.NumberMaximum.readS hort(100), console.Read.Number.readShort(), and subsequent variations for each data type (see the attached image).
Is there a better way to organize my code?
Having upwards of 77 methods in a single "Read" class, with 66 methods being exact duplicates of others with modified data types made it less readable.
Are nested classes a good practice?
I've tried searching online, but all I'm getting are blog post on how static methods are "evil". I have not taken a course on objects yet.
Here's part of my code (error messages and variable names are in French):
I've attached an image of the structure NetBeans displays with my code.
Turn on thread page Beta
[Java] Tips on how to structure code? watch
- Thread Starter
- 29-10-2016 03:22
- 29-10-2016 12:04
Recursively calling the method when an error occurs isn't a good idea because you'd easily run into a stackoverflow exception if the input is invalid once.
- Thread Starter
- 29-10-2016 15:18
Using infinite loops rather than recursion: http://pastebin.com/FmjraDMx
Still, how should I organize those methods? Are nested classes a good practice?