Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Delivering the MVP Validation

Gonzalo Torres del Fierro
PLUS
Gonzalo Torres del Fierro
Courses Plus Student 16,750 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.

tl:dr

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.