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.


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.