How Not Knowing Java Class Vs Object Makes You a Noob – J023

by Jun 25, 2015




DeegeU Java Course

The “How Not Knowing Java Class Vs Object Makes You a Noob” 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 – How Not Knowing Java Class Vs Object Makes You a Noob

Hey there! We covered Java classes in the last lesson. That demonstrated how we can define our own Java types. We also saw what makes a good class. In this lesson, we’re going to do something with our classes. We’re going learn the differences between Java class vs object, and then we’re going to start creating Java objects!

The goals for this lesson are to know what is the difference between a Java class vs a Java object. We will talk about what is the value of a class before you initialize it? And finally we’ll look at how you instantiate Java classes.

Java Class vs Object

So Java class vs object. What’s the difference? In the classes lesson, we showed how classes are the blueprints to create our custom Java types. Classes described a single concept, that might have behavior as well as attributes. We were working with a duck class, but we never really did anything with it. A Java object is the result of using our class template.

Another way to think about it is the blueprints for a house. With blueprints you can build many homes. Every house is one instance of your blueprint. Each instance might have different values for it’s attributes. Like one house might be blue, another green, and so on. All the houses are created from the same blueprint. In Java, a class would be the blueprint and the Java object would be a house instance of the Java class. The class is the type, and the object is the value or instance. The words instance and object are often used interchangeably in Java. But when we declare a Java class, what object value does it start with?

When we worked with primitives, an integer would equal 0 until we assigned it another number. Unfortunately Java doesn’t have a duck lying around in code to use when we define classes. So when we write this, what does myDuck equal? Java has a special value for objects that do not have a class created yet. This value is null.

What is the “null” keyword in Java?

Null is a special keyword that means “nothing”. You can’t do anything with it. It’s not a class. It’s just a placeholder that says, I haven’t created an instance for my class here yet. So when we define a class like this, there’s no object associated with this class. The value of the instance is null. So it starts as null. We need to give our class variable an object value.

To create an instance of a class, we use the keyword “new” followed by the name of the class. We can pass attributes to our class, and well cover that in another lesson. For now, we’ll supply empty parentheses and close our Java statement with a semicolon. This creates a new object of our Java class and assigns it to the variable we define.

You can create more objects using the same class like this. If you have a object that you’ve created, and then you assign it to another object, the original will disappear from view. If nothing else is referencing it, the memory will be reclaimed and the object is gone. Your variable is just a reference that points to some object in memory. Changing the value, changes what it references.

Let’s look at creating classes in code.

Duck myDuck1 = new Duck();

Arrays are Java classes

If you’ve been following along in the lessons, you probably noticed that when we created an array, it used the same syntax. That’s not a coincidence. Arrays are a special type of class. It uses a brace syntax, and has special rules, but it’s really just another class type.

In your editor, you can see the behavior and attributes attached to an array. We’ll cover this more in the next video.

So in this lesson we’ve learned that objects are simply instances of a class. We talked about how a variable of a particular class is initialized to null, until we create an object for it. And finally we learned how to create Java objects.

One last thing I want to leave you with is we’ve just seen how we can take a class and get an object. Can we take an object and figure out what class it belongs to? Yes, and again we’ll cover that soon! In the next lesson, we’re going to give our duck attributes and behavior using Java attributes and methods. If you have any questions or comments about this lesson, please leave your comments below.

See you in the next lesson!







Tools Used

  • Java
  • NetBeans

Media Credits

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

  • Background image from Pixabay.com

Get the code

The complete video lesson list for this free online programming course can be found on the course syllabus page.

The source code for “How to override a method in Java” 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.

Don’t miss another video!

New videos come out every week. Make sure you subscribe!

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