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 The Solution

Solution from video doesn't work.

Main method not found in class Cow, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

is the error message I get every time I try to run the code. Copied solution from video step by step and it still not working. Can't see my mistake. Here's my code

// TODO:  1. Create a new class named 'Cow'.
class Cow {
  // TODO:  2. Create a private variable named 'name'. It should be a String, and once it's set we shouldn't be able to change it.
  private final String name;

  // TODO: 3. Add a constructor that takes a String argument, and in the constructor use that String argument to populate the 'name' variable.
  public Cow(String name) {
    this.name = name;
  }

  // TODO: 4. Create a getter to return the name of the Cow.
  String getName()  {
    return name;
  }
}


import java.io.Console;

// Start by creating a new Cow class in the 'Cow.java' file.

public class Main {

  public static void main(String[] args) {
    Console console = System.console();

    // TODO:  5. Create a new Cow named 'Larry'.
    Cow cow = new Cow("Larry");

    // TODO: 6. Finally, print out the name of your Cow object to make sure everything's working.
    console.printf("The cow is named %s.%n", cow.getName());
  }
}
Teacher Russell
Teacher Russell
16,873 Points

You've made your name variable a final variable, instead of just private. We want to be able to change the value, but a final variable cannot be changed. Don't confuse private with final:) Does that help?

3 Answers

No, it does not. I get the same error. Furthermore from what I see, name variable is set to private final in solution video.

Teacher Russell
Teacher Russell
16,873 Points

https://w.trhou.se/x31gdevwak Sorry, I actually got that part wrong. I never watched the solution, and missed that detail. Here's my code, and you can see my solution. I just realized I never watched or compared it to the video after I did it.

Anthony McClain
Anthony McClain
1,991 Points

Compile Main.java instead of Cow.java

Adam Fields
PLUS
Adam Fields
Courses Plus Student 1,652 Points

I was finally able to get the solution to work in Eclipse IDE first. What should have been a quick 5 minute exercise turned into an hour of scratching my head. I got this to work in Eclipse by removing anything that had to do with console or console.printf and I was able to reproduce the results in the practice workspace. Anyways, here is my code from Eclipse and maybe it can help future students (I wish treehouse would remove everything from this Java course that references importing the console and using it as it wont work with IDE's anyway).

class Cow { private final String name; public Cow(String name) { this.name = name; } public String getName() { return name; } }

public class Main { public static void main(String[] args) { Cow cow = new Cow("Larry"); System.out.printf("The name of your cow is: %s %n", cow.getName()); } }