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 Getting There Class Review

Arnold Rosario
Arnold Rosario
16,357 Points

Do java.util imports go above a package declaration?

I know that for import statements, the java.util imports should be on top. But should they be on top of a package declaration?

Ex: import java.util.Date; package com.treehouse.Treet;

3 Answers

Charles Williams
Charles Williams
2,680 Points

Yeah what I think he meant was that when you are IMPORTING a package, it goes below the Java imports like:

import java.util.Date;
import com.teamtreehouse.Treet;

But when you are DECLARING or creating a package, it goes on top:

package com.teamtreehouse;
import java.util.Date;

AFAIK The first example is just a style preference, but the second one is mandatory or else it won't compile.

Arnold Rosario
Arnold Rosario
16,357 Points

Thanks, so how come in the Class Review video, Craig says that java imports should be on top? Or does it not matter as long as it's organized and readable by others who may view your code?

Charles Williams
Charles Williams
2,680 Points

I believe in the video he says that Java imports go on top for the Example.java:

import java.util.Date;
import com.teamtreehouse.Treet;

But for the class file Treet.java the package declaration goes on top:

package com.teamtreehouse;
import java.util.Date;

I had the import first on Treet.java and it threw an error.

Brett Connolly
Brett Connolly
12,874 Points

Charles helped me fix a problem where Java was saying it was a bad source file

import com.teamtreehouse.Treet; ^ bad source file: ./com/teamtreehouse/Treet.java file does not contain class com.teamtreehouse.Treet Please remove or make sure it appears in the correct subdirectory of the sourcepath.

I guess 'package com.treehouse' must go before import 'java.util.Date' in the Treet.java file even though Craig said in the video that you want to put java imports first, which made me go back and change the order in Treet.java (confused me a little).

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Arnold,

here an example from the KaraokeMashine code:

//on TOP 
package com.teamtreehouse;

import com.teamtreehouse.model.Song;
import com.teamtreehouse.model.SongBook;
import com.teamtreehouse.model.SongRequest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;


public class KaraokeMachine {

}

Grigorij