I've been coding in Java for about six years now (personal projects only).
I follow the... Java, How to Program (Deitel, 5th ed.) book.
For a particular project, I've coded several Record structures, each in their own class file.
Is it possible to put ALL of my Record structures into ONE class file,
but still access each Record structure individually?
If so, how would I accomplish this task?
TIA!

hfx642
Recommended Answers
Jump to Postwell ... a bit far fetched and propably not the most efficiënt way:
if you have a class ClassObject, add a static collection in which you put all the ClassObjects you create.
might be a bit like:
ClassObject.addToList(new ClassObject()); ... ArrayList createdItems = ClassObject.getList();
with …
Jump to PostHe didn't say you couldn't, just that he wouldn't advise it - a sentiment I agree with, by the way.
If you really want to though, you can put them into a single file by making each of the inner classes static.
public class Structures{ public …
Jump to Post@mKorbel: I don't understand your question. Making the inner classes public static allows for instantiation of any of them without an instance of the parent class, so the parent class is really no more than a container for the structures.
Jump to PostAs I stated in my initial post: I am not recommending that he do that. I believe they should be individual class files.
I was merely showing him a way that he could do what he asked about.
Jump to Posttrying to reduce the number of *.class files
Not sure how having static classes will reduce the number of class files.
There should be one for each class, even though there is a single .java source file.
All 20 Replies
stultuske 1,116 Posting Maven Featured Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

hfx642
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

hfx642
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
mKorbel 274 Veteran Poster
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
mKorbel 274 Veteran Poster
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

hfx642
mKorbel 274 Veteran Poster
mKorbel 274 Veteran Poster
cretaros 0 Junior Poster in Training
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
NormR1 563 Posting Sage Team Colleague
Ezzaral commented: Yes, good point. +15
mKorbel 274 Veteran Poster

hfx642
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

hfx642
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.