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 Getting There Packages

Aditya Puri
Aditya Puri
1,080 Points

What does the line "package com.treehouse" mean

What does the line "package com.treehouse" on the top of the file Treet.java mean?

5 Answers

Adam Sawicki
Adam Sawicki
15,967 Points

Because, if you won't do this, compiler will only look for class in the folder where the Main Class is. It won't bother to search through e.g 100 folders you made for your packages. As you can see you provide to the compiler the exact adress of the class so that it can find the class easily where import is needed.

Adam Sawicki
Adam Sawicki
15,967 Points

Directory is structure of folders on your machine, so if the package is com.treehouse you should have structure:

com(folder)--|
             |
       treehouse(folder)--|
                          |
                     Treet.java(file)

Creating a package you should do 2 things : create structure of folders and declare it in the file using package key word.

Aditya Puri
Aditya Puri
1,080 Points

but why do we have to declare it if it was already created in the folder?

Adam Sawicki
Adam Sawicki
15,967 Points

It means that the file "Treet.java" is declared as part of the package. In this case the is directory : "root/com/treehouse/".

Aditya Puri
Aditya Puri
1,080 Points

1) What is directory?

2) Isn't the file already a part of Treet.java as it was declared in the folder?

Adam Sawicki
Adam Sawicki
15,967 Points

You can imagine this declaration as informing compiler about organization of your project. Without this declaration wouldn't even know about existing the package. This declaration actually creates package and structure of folders let compiler find the proper files e.g

you got class named ClassA with declaration:

package com.treehouse;

now compiler will know that ClassA belongs to packege named "com.treehouse" and also it will be informed that the package can be found in directory "com/treehouse". Now When in another class there would be

import com.treehouse.ClassA;

java would look for the class in directory com/treehouse and if it wouldn't find this structure it would be very disappointed, I mean java wouldn't compile that so it's important that packacge declaration should be followed with proper folder structure. Packages are also useful because it creates namespace for your classed I mean you can have two classes named ClassA, but as long as they are in different packages

com.abc.ClassA
com.xyz.ClassA

compiler would treat them as different classes so you don't have to worry about any conflicts.

Aditya Puri
Aditya Puri
1,080 Points

so we can create the class file in 1 folder and declare it as a part of the other folder with the line "package com.folder2;"

Will the class be part of the 2nd folder even when it was made in the 1st one?

Adam Sawicki
Adam Sawicki
15,967 Points

Of course you can do this but your code won't be compiled because compiler won't find desired class where it should be. As I said declaration should be followed by proper folder structure.

Aditya Puri
Aditya Puri
1,080 Points

oh, ok but I still don't get why you declare it to be the part of the folder if 9ts already made in the folder......