Running on Java 19-ea+23-1706 (Preview)
Home of The JavaSpecialists' Newsletter

2.7 Creating a ShuffledWordLibrary

Juppies 2 by Dr Heinz M. Kabutz

We now create a ShuffledWordLibrary by clicking on the WordLibrary abstract class and clicking ALT+Enter, an amazing shortcut that does a lot of heavy lifting in NetBeans (and IntelliJ - Eclipse has a similar magic button CTRL+0). Our ShuffledWordLibrary is not going to contain any words, but will have the functionality of shuffling the words, so that they are not always presented in exactly the same order. After a few games that would get boring.

We add a "field" to the class, which will be another WordLibrary that will contain the words. We declare the field to be final, which requires us to initialize it immediately. We do this by creating a constructor with ALT+Insert. The constructor has the exact same name as the class, but without a return type. If you add a return type (void, int, String, etc.) then it is no longer a constructor, but a method. Whilst we are allowed to have methods with the same name as the class, this is strongly discouraged as it would confuse the reader.

We could have hard-coded the ShuffledWordLibrary to always use the StaticWordLibrary by declaring the field like this:

  private final WordLibrary other = new StaticWordLibrary();

However, then we could only ever have that list of words in our game. What if we want to have a new list? Perhaps read from a file?


When you load these comments, you'll be connected to Disqus. Privacy Statement.

Table of Contents

We hope you enjoyed this tutorial. If you did, you will also enjoy our courses. We suggest you start with Extreme Java - Advanced Java, followed by Extreme Java - Concurrency Performance for Java 8.

About the Author

Heinz Kabutz Java Conference Speaker

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Superpack '22

Superpack '22 Our entire Java Specialists Training in one huge bundle more...

Free Java Book

Dynamic Proxies in Java Book
Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...

Java Emergency?

If your system is down, we will review it for 15 minutes and give you our findings for just 1 € without any obligation.