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
Shariq Shaikh
13,945 PointsWhy do we need to declare the package Treet.java is in if it's already located in the corresponding folder?
Just want some technical insight as to why this step is necessary.
2 Answers
Ryan Ruscett
23,309 PointsA package is a logical grouping. A class belongs to a package when it says package xxx on top of it. There has to be a very tight coupling between packages and classes. So tight in fact that any time you create a new class. All modern IDE's will automatically put the package at the top. It's just what they do.
There is no tighter way than to directly specify a class a part of a package. This means we don't need to rely on directory structure to determine this for us.
If a robber gets into my house and it's pitch black, but doesn't make a sound... How do I know he is there? After all we are both in the same house? Most likely I don't.
If a class is part of a package, but doesn't say so. how does the package know the class is there? After all they share a directory?
You can't have two classes in the same package with the same name. You can't have two methods in the same class with the same name. You can't have two variables inside the same method with the same name. That being said...
I have 10 developers working on one project. Developer 1 has package 1 and all his variables and names. Developer 2 has package 2 and she has all her own variables and names.
Developer 1 never talked to developer 2. How do I ensure, without going through the entire contents of developer 1's package 1. That I developer 2 am not using the same names for a method or a class as developer 1?
I don't know. But surely I don't have time to go through developer 1's packages. Or what if he was gone and I couldn't ask or anything... Well in java it doesn't matter.
Packages have separate name spaces. Methods running in package one with the same name as a method running in package 2 will not conflict and cause a runtime error crashing my program.
That is why it's so important to have a tight coupling between package and class.
Does that help clarify it for you? I did miss the mark the first time my bad.
Ryan Ruscett
23,309 PointsHey,
Not sure what you mean here. Treet.java is not a package. The .java tells me it's a class or a treet object. Meaning some object that contains all the information pertaining to what a treet is. You also do not import classes with .java. It would be import Treet. Now I can access any of the Treet(object) methods by referencing it's name. Treet.getTweet() or whatever.
Now, if this class resides in a different package. You must import that package to gain access to it's class files and import the class(.java) files to gain access to their methods.
Does this help at all lol?
Shariq Shaikh
13,945 PointsNever said Treet.java was a package, to clarify my question: At the top of Treet.java, why do we have to include "package com.threehouse;" if Treet.java is already in treehouse which is com?