Running on Java 17+35-2724 (Preview)
Home of The JavaSpecialists' Newsletter

3.24 Making ArrayShuffler.shuffle() Methods static

Juppies 2 by Dr Heinz M. Kabutz

Something that has been bothering me is that we need to create an instance of the ArrayShuffler each time we want to shuffle, even though the ArrayShuffler has no fields and therefore no state. There does not seem to be a good compelling reason to do it this way. Let's change the two shuffle() methods to be static. This means that we do not need to make an object of ArrayShuffler in order to call the methods. However, the rest of our project will still run. Even though it is not recommended, we can call static methods on objects of that type. The reason that it is not recommended is that inheritance works a bit differently with normal methods than with static methods. It is less confusing when we call the static methods on the classes directly.

There is one special case where calling a static method on an object will not work, and that is with method references. Fortunately our test shows that place, and we fix that to call ArrayShuffler::shuffle.

Comments

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 21

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

Free Java Course

Free Juppies 2 Course
Juppies 2 - a course for complete beginners 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.