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 Meet Objects Final

João Ignácio Brito
João Ignácio Brito
1,201 Points

Creating classes

Hello, I am !trying! to play around, creating classes and trying to use them but I am not having succes.... Can anyone help me?

class MeuNome {
    public String firstName;
    public String secondName;
  public Nome() {
   firstName = "João"
   secondName = "Ignácio"  
  }    
} 

public class Example {

  public static void main(String[] args) {
    // Your amazing code goes here...
    Nome Eu = new Nome();
    System.out.printf("Hi my name is %s", Eu);


  }

}

```java

2 Answers

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Hi Joao,

Good try - you did better than I probably did my first time writing classes:).

Here is my solution. A few notes:

1) Each class needs to be in it's own file in order for it to compile. So you need an Example.java file and a MeuNome.java file.

2) The constructor for a class needs to have the same name as the class. So, the MeuNome class' constructor has to have the name MeuNome. Nome won't work.

3) Add ";" at the end of field assignments inside your MeuNome constructor and qualify the fields with the this keyword to reference the MeuNome object you're creating with the MeuNome constructor. So, instead of:

firstName = "João"
secondName = "Ignácio"

you want:

this.firstName = "João";
this.secondName = "Ignácio"; 

I hope this helps, let me know if you have any more questions!

Example.java

public class Example {

  public static void main(String[] args) {
    // Your amazing code goes here...
    MeuNome Eu = new MeuNome();
    System.out.printf("Hi my name is %s", Eu.firstName);
  }

}

MeuNome.java

public class MeuNome {
    public String firstName;
    public String secondName;

    public MeuNome() {
        this.firstName = "João";
        this.secondName = "Ignácio"; 
    }    
} 
João Ignácio Brito
João Ignácio Brito
1,201 Points

Thanks for the reply Chris!! Ohh it's good to know that I'm not the only one that had problems creating classes for the first time! Now it works!! Again, thank you for your help!

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Not a problem, Joao! Object oriented programming and classes is a different way of thinking, so it takes some time to learn to think that way. You're definitely not alone. Be patient with yourself and keep at it and it will start to make more and more sense.