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 Creating Classes

Problem with class! confused, need help.

Okay guys, When Craig create the class with the name PezDispenser and uses later on the file Example.java........ What is he actually using. is he using the PezDispenser.java or the class PezDispenser. this is something is not clear for me, because when he created the class, he named it with the same name as the file ------> PezDispenser.java. I was doing great, I guess! because I was following his work, but I want to understand the concepts. this is what I'm talking about, this code does not compile, finds two errors. but when I put the class he wrote, and basically everything he's done. then, it works! I'm scared no to understand after I found myself into this :/

public class Example {

  public static void main(String[] args) {

class LukasCaleb {
  String characterName = "Yoda";
}



    // Your amazing code goes here...

    System.out.println("We are making a new Toy");
    LukasCaleb scraig = new LukasCaleb(); 
    System.out.printf("The toy we are making is %s %n", scraig.characterName);
  } 

}

1 Answer

Darel Bitsy
Darel Bitsy
6,910 Points

First class file LukasCaled

class LukasCaled {
    String characterName = "Yoda";
}

Second class file Example.java public class Example {

public static void main(String[] args) {

System.out.println("We are making a new Toy");
// Creating a LukasCaled instance
LukasCaleb scraig = new LukasCaleb(); 
//Accessing the characterName field from the LukasCaleb class
System.out.printf("The toy we are making is %s %n", scraig.characterName);

}

}

// I think your code doesnt compile because you defined the LukasCaled class inside the example java file , if you wanted to define LucasCaled class inside the example file so check your indentation , you could do it like that

public class Example {

public static void main(String[] args) {

class LukasCaleb {
    String characterName = "Yoda";
}

// Your amazing code goes here...

System.out.println("We are making a new Toy");
LukasCaleb scraig = new LukasCaleb(); 
System.out.printf("The toy we are making is %s %n", scraig.characterName);

}

}

Seth Kroger
Seth Kroger
56,413 Points

While you can have a class defined inside another, called an "inner class", they need to be defined outside of any methods. Basically at the same level as fields and methods. Inner classes are beyond the scope of this course but are a handy thing to know about.