ABOUT THIS DOCUMENT

 

On the CCC, you must submit a single file for your project.  Below, is a brief explanation on how to use multiple classes in one file.  Thank you to Connor Killingbeck who discovered the need for this knowledge, then researched how to do this and then created this guide for us.

 

*****

 

MULTIPLE CLASSES IN ONE FILE
by Connor Killingbeck

 

We often use multiple classes while coding in Java. Classes are often used to create objects, or in some cases to keep functions organized. In most cases, having multiple class files is not an issue, however, there are some scenarios where only one class file can be used. A good example is during coding competitions, where you are only allowed to submit one file. In this scenario, how would one go about creating objects or organizing functions?

 

This is where the concept of “Helper” classes comes into play.

 

Helper classes are classes that reside INSIDE of the same class file. These classes are accessible by anything in the same package as well!. For example, if we have file A and file B, File A’s helper classes can be accessed by anything inside file A or file B! Though this aspect will rarely ever be used.

 

Here is an example of a helper class inside of your average class file:

 

https://lh3.googleusercontent.com/EHbpoCJU-Bi4LV2AmR3FccEP8YGawtfKLVIsLMkPkL6u6jxckfle3Iuu9pGTrNJoCOMUKnmWvhTnKtjRJFooWlasN6gk1yYsU-j5R3282ZA5Zr-uUh4d5iVupw9qRMt6N7Jmw_qo

 

To use a helper class, simply add [class ClassName] followed by curly brackets { }.

 

Not that these classes CAN NOT be set to private or public.

 

These classes act like normal classes, we can add functions to them, create constructors, ect. Here is a plausible example of the use of helper classes:

 

https://lh4.googleusercontent.com/jXglhZtBf5tAfM213P9hAknAOKUoWhf2-jtNCMuA7b60dZC5jPhCvXULs32lw0q_qRD9BSnPSGiNEV7nR0p_t5TkG-hnZxFBzrsMpBTyHHWzxCgYcJ5ayM_ymN5l2H2G2Osh3x1R

https://lh4.googleusercontent.com/53EiwiFY3tyviseotuDYNFtlPoC5N06Ca9dM-ynPUYk_zmmvJtKkb1TbreD4Q68Td04MGWbNxxaV5WgOWBkZlEa4Tu8p_RK33quXyeLxgBVptQipz2oGoMtxTgh8LSTzMRsoboxA

 

In this scenario we created objects using Helper, and then kept our functions cleaner using HelperFunctions. Of these two, creating objects is by far the most useful, but just maybe the clean freak in you might like the organizational assist that helper classes can add!