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

When To Create an Instance of an Imported Class

Hi Treehouse, In this video Craig creates a new instance of the java.util.Random class he imported at the top of the file

import java.util.Random;

....

Random random = new Random();

Why didn't he create new instances when he imported 'java.util.Arrays' or 'java.util.Comparator'?

1 Answer

Brendon Butler
Brendon Butler
4,254 Points

The simple answer is that you can't.

The top part of a class, before your class declaration is there for reference purposes. This is where you would put imports and package declarations. It's essentially used to tell the compiler what classes are used within a java file.

Here are some references to take a look at:

Java: Packages and Import | Java Imports | Java Objects and Classes | What exactly is an instance in java?