NEVER create Java objects in an unknown state! – J033

by Oct 21, 2015




DeegeU Java Course

The “NEVER create Java objects in an unknown state!” video is part of a larger free online class called “Free Java Course Online”. You can find more information about this class on “Free Java Course Online” syllabus.

Transcript – NEVER create Java objects in an unknown state!

Hi everyone! If we were creating an American football game in Java, the score would be easy to model. The initial values for the score are automatically initialized to be zero. What about the starting position of the football for kickoffs? The ball starts on the kicking team’s 35 yard line. How do we do that in Java?

We could set our football game instance after we create it, but that leaves our game instance in an invalid state until we set the football position. This could create hard to find errors if we forget to set the value correctly. Like if we forget to set the away team when using the class. A best practice is to create our Java instances in a usable state. In this lesson we are going to look at Java class creation and learn how we can create Java instances in a useable state when we create them.

The first thing to think about are primitive values. I said in a previous video that primitive numbers are initialized to zero. That seems obvious, but in our football game there’s no zero yard line. That won’t work for us. Zero is not a valid state. Classes like String, of course, are initialized to null. We’ve seen what happens when you try to use a null instance. The string classes will be null at creation. That won’t work either.

During Java class creation, after the class is created but before we can use it, Java calls the constructor for the class. The constructor is a special method of the class. In Java, the constructor is always named after the name of the class. It doesn’t need a return type, and we do not need to call return in the constructor.

We usually place the constructors at the start of the class, but that’s just a best practice. While we can put them anywhere in the file, we really want them easily found at the top. That’s where other programmers will look for constructors.

Inside the constructor, we can perform any required class initialization. This is one possible place we can initialize our values. We’ll cover others in the next few videos.

To initialize our football game class, we’d set the ballPosition to 35 and the teams we’d initialize to the particular teams playing the game. Of course we’d specify much more for a football game, like who has the ball, what down it is, etc. This is just a simple Java object creation example.

Now in this example, we’re forced to decide the teams in advance. That’s not very flexible. We need to specify the teams when we create the game instance. We can do that by adding another constructor with parameters.

To create our class, we’d call it like this. This allows us to specify the teams when we create the game.

So now we have two constructors, both return a game instance in a usable state. Also notice we still set the kickoff position value to 35 in both of our constructors. The game rules used to start the game at the 30. That rule changed recently, last year I think. If they changed the yard line again, we’d need to change every constructor. Not good. We might miss one. Remember, while you should put constructors at the top, they can be anywhere in the file.

A better idea is to chain your constructors, so one calls the next. We’ll look at chaining constructors in the next video.

Thanks for watching! If you have any questions let me know in the comments. New videos come out every week, so make sure you subscribe. You don’t want to miss a video! And with that, I’ll see you in the next tutorial!

<p><script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- DeegeU - Right Side -->
<ins class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-5305511207032009" data-ad-slot="5596823779"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></p>

Tools Used

  • Java
  • NetBeans

Media Credits

All media created and owned by DJ Spiess unless listed below.

  • No infringement intended

Get the code

The source code for “NEVER create Java objects in an unknown state!” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:

 git clone https://github.com/deege/deegeu-java-intro.git

Go to the Support > Getting the Code page for more help.

If you find any errors in the code, feel free to let me know or issue a pull request in Git.

<h2>Don't miss another video!</h2> <p>New videos come out every week. Make sure you subscribe!<br><script src="//apis.google.com/js/platform.js"></script></p> <div class="g-ytsubscribe" data-channel="deegeu" data-layout="full" data-count="default"></div> <p></p>

Comments

comments

DJ Spiess

DJ Spiess

Your personal instructor

My name is DJ Spiess and I’m a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I’ve programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I’ve worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.

Pin It on Pinterest

Share This