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

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 classes vs objects, 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. talk about what is the value of a class before you initialize it. And finally we’ll look at how you instantiate Java classes. 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 its 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. Null is a special keyword that means “nothing”. The keyword is all lower case
letters. 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 myDuck is null. When we define classes without assigning an instance to the variable, 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. This creates a Java object in memory. It doesn’t exist until we use the keyword new. 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 statement creates a new object of our Java class in memory and assigns it to the variable. So this half defines the variable, and the other half creates the object. The
assignment operator connects the two together. This statement says define a variable called “myDuck”, and create and assign a new duck instance to the variable. You can create more objects using the same class like this.
This creates four duck objects from the Duck class. 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. In this example the duck created in the second line is the only duck. It’s like if you had a variable that was 1, and then you set it to 42.
Let’s look at creating classes in code. So this is the first way to instantiate
a Java class. There’s several others that we’ll cover in
subsequent lessons, but this is by far the most common way to instantiate a Java class. We’re going to use the Duck class from
the last lesson. If you remember, it was just “public class Duck”. To
instantiate a new duck, we just do duck as our variable name,
equals new Duck(). Note that the class name is capitalized,
but the variable name is lower case. And then we use the
keyword “new” with the class name Duck. And this creates our new duck.
So if we add System.out.println(duck), what is this application going to print? We run it, and it’s going to print out something a
little weird. It’s com.deegeu.classes.Duck with some number. That number is actually the reference number for your object. I told you before, if we come
in and created another Duck. Let’s put a println in between the two of them.
Now when we print it out, noticed the number at the end is changed.
The reason is the Duck class that we created on line 13 is erased by the Duck class that’s
created in line 15. So duck has a new instance it is
pointing to in line 15. When it prints it out, it prints a different value. 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! Hey!
Thanks for watching the video. There is a quick quiz for this on if you’d
like to gauge how much you learned. If you like the videos are seeing, please
let me know by liking the video and hitting the subscribe button to the DeegeU channel on YouTube. I’d really
appreciate that. If you have concerns or questions, please leave them in the
comments below or on There’s a poll on
the front page of so you can let me know
the topic that is covered next. Thanks for watching, and see you in the next video.


  1. Junaid Tanoli

    August 27, 2015 at 5:22 pm

    really nice explanation sir, can you please add beginners tutorials keywords to get more views please, you should have more views , do some SEO stuff . thanks

  2. Junaid Tanoli

    August 27, 2015 at 6:32 pm

    please zoom console/output area for clear reading . ty

  3. Jalal Dad

    April 28, 2016 at 8:19 am

    Great. Now I'm understanding java terminologies. Thanks

  4. Alex

    October 4, 2016 at 9:13 pm

    Thoroughly enjoying this series of videos! You explain things in an engaging way, accompanied by easy to understand examples and first rate video editing/production values.

  5. barnesdouglass

    May 8, 2017 at 7:31 am

    This was a great tutorial, thanks !

  6. Sarah Faller

    June 7, 2018 at 6:19 pm

    When you declare a new variable that holds an int, you dont need the key word "new" is this bcause it's a primitive type? If so, when you declare a new string, you also don't need to do it. But it is an object I believe. I am a bit confused!

  7. osuji paul

    June 24, 2018 at 7:57 pm

    Another wonderful you tube Teacher. I love your lesson very simple well explained and easy to follow

  8. Razor M

    July 12, 2018 at 1:29 pm

    ∞ thanks deege u, awesome tutorial

  9. TheSnowBall

    May 9, 2019 at 2:33 pm

    What's the music in this video?

Leave a Reply