Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects (Retired) Delivering the MVP Validation

Gonzalo Torres del Fierro
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

Hangman project

why on the project named Hangman, we have 3 .java, i mean the prompter, the main, and the game?, i can see, this an interaction, but i get lost trying to understand, wich one get the "logic" and when i have to go and make a new function, or method to complete the whole structure.....some times i start coding on the wrong "place.java" ...

3 Answers

Pretty much all Java programs, aside from very small ones (like Hello World), are written with multiple files/classes. It is part of Object Oriented Programming, and just generally is a better way to split up code. It's just something you are going to have to get used to.

Edit: Derek beat me with a much better explanation. :p

Well, when just writing pure java source code and your project contains more than one java class. The only code that gets invoked is the code inside of the main method.

public class Example {
  public static void main(String[] args) {
    //only the code inside of this method is ran at compile time.

When you have more than one class, such as in the Hangman project, each of the classes is meant to handle different logic. You have a Prompter class, a Main class, and the actual Hangman game class. Your Main class will be the class where you have your main method(There can only be one class that includes the main method, in a project that includes more than one class.).

It's worth noting, that you could technically have another main method, just not one that takes a String array as the parameter

Without seeing the actual code I couldn't tell you what each class is doing exactly. But, the reason I bring up the main method is because your Main class will most likely be the class that contains the main method, in which you invoke each of the required methods from your Prompter and Hangman class to make the game actually run.


Basically each class does a specific job in your project, while you could technically write all of the code necessary in one class, this quickly becomes very messy due to all of the code. It's much better practice to separate specific logic into their own java class, then invoke those methods inside of the main method to make the game actually run.

Let me know if this is confusing and I could try to re-explain a different way.