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 trialYoussef Khaky
1,871 PointsProblem understanding why we need to import twice
So we already imported the Java date class in treet class. Now when we import the treet class into the example class, why do we need to import the date again. Why is it not imported along with what is imported from the treet class. (Also I may be mixing up terms I don't know what's the difference between treet.java and a treet class, is someone can elaborate on that I would be thankful) Thanks you.
3 Answers
Mahmoud Yousif
5,053 PointsFirstly, i'm going to explain the difference between Treet.java and Treet Class. Simply put, the difference is that a class is like a blueprint in which your methods and member variables describe what a "Treet" is. Treet.java is the file that stores said blueprint instructions.
Secondly, an instance of the Date class needs to be imported every time you use it since it carries it's own methods that are apart from Treet class. Those methods don't get included in the imports of the Treet class.
Good luck and I hope this helps :).
Wesley Wright
26,793 PointsAny time you use an external file or library, you have to import it. You use the Date library to create a new date object as a part of your new Treet(I'm assuming you do... haven't done this lesson), so you have to import the date library as well.
treet.java is the file containing the treet class.
As far as the why, I couldn't tell you. I guess it's the way Java and a lot of other programming languages are set up. You will find that is pretty consistent across other programming languages.
Austin Bynum
1,741 Pointstbh, i'm not sure this is the correct answer but this is what i've found on the subject....
coming from a php background...i was having a hell of a time with this too. i was confusing import with include. include inserts the entire contents of the file you referenced. i assumed that is what was supposed to be happening here, until we got to this lesson, where i too asked myself the same question you did. i imported the date class in the package i imported....so why do i need to import it again?
after some research i discovered that import is much different than the include i was used to. this research also explained the answer to our question of why we need to import it twice as well. include does take the contents of the entire file you reference, but the key here is that it does that before any processing is done. it essentially copies and pastes the contents of the file at the point you import it, then runs all that code at once.
import is different because it actually evaluates the code first. so when it does it's pre-processing, the date class and it's methods are there and available. however when you import that package, the date class and it's methods aren't included with the evaluation. so as i now understand it, the import command actually imports an evaluation of the code, not the code itself.
again, i'm new to java and it's been quite awhile since i've touched any code, so this may be incorrect. any clarifications from the pros would be appreciated.