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 trialSalman Ramadhan
3,446 PointsWhen we create a java program, do we need to write a class for our program?
When we create a java program, do we need to write a class for our program? what i know is class in java is generates when we compile our java file.
3 Answers
Jeremy Hill
29,567 PointsWhen you write a java program it is just a .java file. Once you successfully compile your .java file the jvm will automatically create a .class file for you. That class file is actually the byte code that the computer uses to run your program. To put it in easier terms: The Java Virtual Machine (JVM) takes the code that you understand and compiles it into code that the computer understands (byte code) and the end result is the .class file.
Jeremy Hill
29,567 PointsYes just write the Java code then make sure that it gets compiled before you try running it: javac className.java
Then run your code:
java className
This will automatically produce your class file; and if you ever lose your class file or it gets corrupted then just compile your Java code again and it will produce another class file and that's all there is to it.
Salman Ramadhan
3,446 Pointsok thank you very much for your explanation
Jeremy Hill
29,567 PointsYou're welcome, after you compile your next program check the same directory as your .java file and your .class should be there too. Unless you changed the file path for your .class file but I wouldn't do that until you are more experienced.
Salman Ramadhan
3,446 PointsSalman Ramadhan
3,446 Pointsso, I'll just have to write from the java file?