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
Erin Leathers
3,562 PointsClass in Java
So a Class is a "blueprint"
Are there two types of Classes ? User Defined ? and ones that come with the complier ?
I would believe so since MAIN class is always created ?
Can you provide syntax for both ?
Easy ways to spot in the code ?
and maybe general anaolgies about the class ?
I know the technical definition , I would just like easy ways to remeber it as i'm looking at code.
Thanks !
3 Answers
Aaron Arkie
5,345 PointsHello Erin, You are correct a Class is a blueprint. But it is a blueprint for the object you are trying to create. There are classes that you, the user will create. But there are also classes that are defined in what's called the java library which have been created by users that are deemed useful and important. These sub classes allow you to easily find and create things and make happen that you would have had to create yourself by hand. These classes essentially allow you to "use the right tool for the right job" and makes things less of a hassle. You will often see "import Java.awt.Color*;" or something of the sort with "import". This allows you to import that class from the library. In my example you are importing from the java library a package called awt and asking to use the class called Color.
Furthermore, MAIN is not a class but a method that the Java Virtual Machine will look for at run-time. There needs to be one class in your java application that has one main method. This is a little complex if you are just starting to learn the java language but you should know that the main() method is where your program will start running.
Here are some ways you can remember this( Straight from the book, Head First : Java by Kathy Sierra & Bert Bates).
So a Class is a "blueprint"? A class is like a recipe. Objects are like cookies.
Are there two types of Classes ? User Defined ? and ones that come with the complier ?
Roses are red, apples are ripe, if you don't import, you'll just have to type.
So in code form to demonstrate a Java application that has two classes and a main method. (Remember you can only have ONE main method in any application)
public class MainMethodDemo {
public static void main(String[] args) {
//here my main method will allow all of the magic to happen.
Dog Beagle = new Dog(); // Here i am creating an Object reference variable for the class Dog named myDog.
Beagle.speak(); // Now that i have control of the Dog class i can use its method called speak()
}
}
Here is the Dog class.
public class Dog {
public void speak() {
System.out.println("Bark Bark");// tells the console to display this message
}
public void wagTail() {
// code to make dog wagTail
}
public void play() {
// code to make dog play
}
public void growl() {
// code to make dog growl
}
public void sit() {
// code to make dog sit
}
}
I hope this helps you out! The best advice someone ever gave me to start learning programming is to pick just ONE language and immerse yourself into. Live and breath the language then you will see if you like it or not. If not then choose a different one. Coding is really unique and is great for improving problem solving skills. Good luck, stay with it!
Erin Leathers
3,562 PointsThank you for the reply, I enjoyed it. I didn't realize that the import was of a class.
Still shaky about the object, going to re read your post a few times.
Aaron Arkie
5,345 PointsNot a problem, let me know if you need me to clarify anything. If you really want to learn the basics of the language i highly recommend Head First: Java by Kathy Sierra and Bert Bates. And for reference Java: A Beginners Guide by Herbert Schildt. (I have the 5th edition but i suggest getting the most current one). I use these books very often and they come in handy when i am just lost on how something is done or i need a concrete definition.
Erin Leathers
3,562 PointsUpdate:
Aaron I took your suggestion and started reading Head First Java and also Thinking in Java. I took about a week off of Treehouse and read , did the practical exercises (I loved writing code in Note pad and running in CMD window !) Tonight I returned to Treehouse and passed on of the classes I was stuck on !!!! I am slowly starting to understand the concepts. My goal is to really understand Java and then progress to android so thus the fundamentals are really important to me. Thanks very much for your suggestions!