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 PostIs this just a theoretical question, or is there some particular problem that you are trying to solve? If so, there may be other better ways to approach it.
Jump to PostIn that case I would say don't do it. When it comes to reading or modifying code its far easier to deal with many well-named class files than one gigantic file.
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 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.