Member Avatar for hfx642

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!

Recommended Answers

All 20 Replies

well ... 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 addToList and getList are static methods, that can work with the static list in the file.

I must say, though, my choice would be to keep the collection in a separate file.

Is 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.

Member Avatar for hfx642

I'm just trying to reduce the number of *.class files to handle record structures.
A single *.class file to handle all of my record structures.

In 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.

Member Avatar for hfx642

Normally, I would agree with you!
But, once I know that a data file is being read and written correctly,
I would rather put all of that code into a single file (and *.class) and not bother with it again.
Anyways... I was just hoping.

He 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 static class FirstFormat{    }
    public static class SecondFormat{    }
}

and access them like

Structures.FirstFormat recStruct = new Structures.FirstFormat();

I think you're better off keeping each in a separate file and just putting them into a single package. Later, if you need to share them with another project, put them in their own jar file that you can include as needed.

@Ezzaral

making each of the inner classes static.

not sure with that, where you ensure about that, why public static or protected,

(maybe final but that refuse my IDE with little bit crypted warning, remove final modifier from class definition)

@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.

I thing that's not good ..., because you forgot to tell him, that any changes equals reinitialize whole class,

only for Aplications static variables, once loaded and never changed

again my question why inner public static (or protected) class, I think that OP means aplications midlle tier, for basic data exchange, that would be beeter to change public static class to the public String(with return)

anyway agreed, correct answer doesn't exist because questions are very vague,

As 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.

Member Avatar for hfx642

He 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 static class FirstFormat{    }
    public static class SecondFormat{    }
}

and access them like

Structures.FirstFormat recStruct = new Structures.FirstFormat();

I think you're better off keeping each in a separate file and just putting them into a single package. Later, if you need to share them with another project, put them in their own jar file that you can include as needed.

Ooooo! I like that.
That's the sort of thing that I was looking for.
I'll give that a try. Thank-You!
ps. I don't share my record structures between projects.

@ Ezzaral

:-)

@ hfx642

advice is advice

but, in other hands

myButton class (nothing Custom, just lots of Contructors with variable amount of pointers) have got more than 1T lines, I'm so lazy, my endless lazyness doesn't allows me wrote something twice, wrote once and use forever

and JButton is very simle JComponent ...

Why dont you group them using tabbedPanes, whereby you will have absratc or innerclasses, therefore you will only have one .java containing all classes you have.

follow this link and see an example of what im saying:

http://www.beginner-java-tutorial.com/jtabbedpane.html

The question has nothing to do with UI components. It's a class structure issue.

trying 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.

commented: Yes, good point. +15

is absolutely one in this debate, whether it is the UI, or the Abstract assumption based on data loaded,

ok, if the method is used more times from different classes should have their own class, if not then is absolutely one if has 100 or 2T lines, agreed

Member Avatar for hfx642

Ezzaral...

Sorry to report, but your solution did not work.
It actually created 3 class files, two of them being $ class files of the outer class.

Now... If someone can figure out how to get the $ class files into the outer class file, then were cooking with electicity!

You can't. You can push them all into a single .java file, but the compiler is going to break them up again as you have seen (and NormR1 mentioned above).

You can't make separate classes not be separate classes. Sorry I didn't make a distinction above. I meant a single java code file.

Member Avatar for hfx642

Oh, well... Thanks anyways!

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.