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 Data Structures - Retired Getting There Packages

Aditya Puri
Aditya Puri
1,080 Points

Why did we have a new class file?

When we ran the program, why did a new class file(Treet.class) got created?

1 Answer

Harry James
Harry James
14,780 Points

Hey Aditya,

This happens when we use the javac command, when the compiler does the work for us :)

This converts the code we have written into a smaller file, that is more efficient to be run on devices.


Hope it helps and if you have any more questions, give me a shout :)

Aditya Puri
Aditya Puri
1,080 Points

how can we use that smaller file?

Harry James
Harry James
14,780 Points

Hey Aditya,

We can't actually use this ourselves, as to us it isn't a readable format. Java knows how to read it for us though, so we'd just call it with java Treet.

Note that this would actually throw an error as we're trying to run a class without a main() method. However, it's the same thing we're doing when we call java Example, we're telling Java to run the Example.class file rather than the Example.java file.

So, to summarize:

  • When programming, we use the .java files, which are in a human-readable format.
  • When running the program, we use the javac command to compile our human-readable code into a smaller, more efficient file, the .class file. This code is readable to the computer, not us, and provides binary instructions for how to operate.
  • Finally, we use the java command to run the code that is readable to the computer - the .class files.

Hopefully this explains things but if you're not quite there, let me know :)

Aditya Puri
Aditya Puri
1,080 Points

So we can delete that file? Is it just a binary form of the file that had been runned? If we run our program multiple times then wouldn't a lot of these files clutter in our folders and make everything look messy?

Harry James
Harry James
14,780 Points

Hey again Aditya,

The file will overwrite itself whenever you run javac again, preventing the clutter you're on about :)

Also just to clarify, it's not a binary file of a program that has been run, it's a binary file of a program that has been compiled. Even if you were to just use the command javac without java (Compiling, not running), these files would still be created.

Aditya Puri
Aditya Puri
1,080 Points

so if we delete them and then compile again, then they would be recreated? And if we compile again without deleting then the existing one would be rewritten?

Harry James
Harry James
14,780 Points

Yep! You got it :)