0

Is it possible to loop through all Java files in a given directory and create a Java Object from each of those files? Assume all files are Classes.

Example: Assume I have a directory named "playing_cards", and each file in this directory correlates with one of the 52 playing cards and they all inherit the same Class: PlayingCard.

List<PlayingCard> deck;
for(File file: playing_cards){
  deck.add(new PlayingCard(file.path()));
}
Jerms3033
  • 91
  • 3
  • 10
  • First qurestion: Why? Second question: Does this solve your problem ? https://stackoverflow.com/questions/205573/at-runtime-find-all-classes-in-a-java-application-that-extend-a-base-class – Marko Taht Sep 23 '22 at 20:44
  • Yes, but it's going to be a very advanced solution involving reflection. Is there a reason all 52 playing cards are of separate class types? It would make far more sense to have one `PlayingCard` class which takes constructor arguments (from enums, perhaps) – Silvio Mayolo Sep 23 '22 at 20:45

0 Answers0