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 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: 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: 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! |