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 Efficiency! Design the UI

Derek Derek
Derek Derek
8,744 Points

importing packages

Hello,

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;