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

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?