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


Kaytlynn Ransom
Kaytlynn Ransom
Python Web Development Techdegree Student 11,409 Points

Using getInstance() and nextInt()

Hey guys!

I'm rewriting a program that creates a simple version of the dice game 'craps'. Looking over the code, I found this line:

int value;
value = RandomGenerator.getInstance().nextInt(1,6);

The purpose of the code here is to set the number on the dice. I'm a little confused about the use of getInstance() here. All of my sources say that getInstance() is used mainly in connection with the Gregorian calendar. Any ideas as to why it's necessary or being used to retrieve the number generated by nextInt()?


1 Answer

Dekel Zoaretz
Dekel Zoaretz
14,864 Points

This way of writing is used to create a singleton class. you can read more about it in the following article: The Singleton pattern but the general idea is that a class will be instantiate only once and the getInstance method is the way of getting that instance that was instantiated at the first time.