Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Data Structures Efficiency! Design the UI

Derek Derek
Derek Derek
8,744 Points

importing packages


I'm not sure about the relationship between the directories/folders in which the .java files are saved and the package commands on each of the files.

For instance,

for Song.java and SongBook.java, it is "package com.teamtreehouse.model;" and these are stored in com/teamtreehouse/model.

for Karaoke.java it imports both of the files in com/teamtreehouse.model "import com.teamtreehouse.model.Song;" "import com.tremtreehouse.model.SongBook;"

for KaraokeMachine.java which is saved in com/teamtreehouse, it is "package com.teamtreehouse;"

My questions are

  1. why do the files have to be saved in different directories and how do we determine which one has to go where
  2. how does the location in which they are saved change the import package lines?

Thank you!

Jasper Peijer
Jasper Peijer
45,009 Points
  1. Files are saved in different locations for accesibility. For example: If you want some certain classes have accessibility to an method in another class and some not, You give the method protected instead of public or private. This means it is only accessible from the same package.
  2. The import package line is basically what you wanna import and in which package is it located. So if you are in a class in the package com.teamtreehouse.model and you wanna import for example the class Apple from the package com.teamtreehouse (NOT in com.teamtreehouse.model). Then you wanna use this:

import com.teamtreehouse.Apple;