Practice English Speaking&Listening with: [Classes, Video 2b] Defining and Instantianing Classes

Difficulty: 0

The key language feature that we're

going to use to make it more natural to represent

the entire universe of dogs or whatever

else, is that classes in Java can contain not just functions,

but also data.

So what I mean by that is that we

are going to augment our Dog class so that dogs

have properties, like size.

And that class is going to serve as the blueprint for all dogs

out there.

And then we're going to instantiate that class

into specific objects that we can then manipulate.

Now, that will make more sense once we actually

try out an example.

So let's jump right in.

So hopefully, you've seen something pretty much

like this.

Now, I know some of the people taking this class

are coming from a MATLAB only background.

I have a little example here that you

can read to get a sense of what this is all about.

But let's try it out in Java and see what it's

like to make a dog blueprint.

So what I'm going to do, I'll reopen my Dog class.

And I'm going to say a dog is not just

a noise-making machine.

Actually, a dog also has a property.

That property is a dog is going to have, let's say,

a weightInPounds.

So as an American, I use the bizarre pounds system

as opposed to kilograms.

We're just going to roll with it.

And that specifies how big a dog is.

Then what I'm going to do is, I'm

going to change the MakeNoise method.

And it's going to have a few cases.

So if the weight in pounds of a dog is less than say 10,

we're going to say it's going to yip.

Otherwise if the weight in pounds less than 30, then

we will say that it barks.

Otherwise it's such a large dog, that it has a woof sound.

So there's no Malamutes here, like that dog

that we had a beautiful ow-ooh.

So we'll default to woof for large dogs.

So that's the makeNoise method.

And now if we try and compile our Dog class,

I get this funny message.

Non-static variable weightInPounds cannot be

referenced from a static context.

So we'll get a better sense of what that means,

but I'm going to tell you that the simple solution for us

right now-- this is not only the right solution when we get this

compiler message--

but what I'm going to do here is just

delete the word static and pick apart why that is soon.

Now if I try to compile it, compiles.

And this is now a blueprint for all dogs.

It can have different behavior based on the weightInPounds

of the dog.

And it's just a glorious thing.

So let's go into dogLauncher and see what things are like.

So we now try to compile dogLauncher,

we get non-static method makeNoise cannot be referenced

from a static context.

So we might be tempted to delete the word static.

That is not the right answer here.

What we want to do instead-- and this is going to be a new piece

of syntax--

I'm going to say I need a specific dog.

So I'm going to say this is totally new syntax--

Dog d equals new Dog.

This means create a dog, a new dog.

And let's call that dog in this case d.

Then what I'm going to do is say d.weightInPounds

equals let's say 25.

So the dog now gains a certain weight.

And then instead of saying Dog not makeNoise,

I'm going to d makeNoise.

That's a really important point.

So this is like saying great sky dog, the plutonic ideal

of all dogs, please make a noise.

But if we look at the Dog class, this

is expecting a specific dog to be making noise.

It's saying, if my weight, if me-- me, my, or whoever,


if my weight in pounds is less than 10, I'm going to do this

and so forth.

So instead, what I need to do is reference a specific dog.

In fact in this case, it's the one I just made.

So if I do javac

and then do java DogLauncher, I get bark,

because it's a 25 pound dog.

If I do 51, and I recompile and run it, then I get woof.

So what this piece of code does is it creates a dog,

sets its weight, and then makes that specific dog make noise.

And we'll be picking more carefully what static means

apart in a minute, but I just want

to make sure this syntax is clear for now.

So what I'm going to do is one last little tweak

to this Dog class and DogLauncher

class before I move on and kind of reflect on what we've done.

So for those of you with some object-oriented programming

experience, which is hopefully almost everybody,

you're probably used to the idea that setting variables

like this manually is kind of strange.

So more typically what we'll do is the DogLauncher class, when

we create the Dog, we'll actually

give it as a parameter to the Dog Creation process.


So I'm going to get rid of this.

And so in order to enable this--

I mean if I try right now, it's not going to work.

It's going to say constructor class and Dog cannot be applied

to given types.

So what it's saying is, I don't know

what to do with this number.

So to fix that, what we're going to do

is create a special thing, kind of like a method,

known as a constructor.

So I'm just going to say public Dog int w.

And this is the constructor for we'll call it the One integer

constructor for Dogs.

So what that's going to do is if I say new Dog

and give an integer, this constructor will get called.

And that will specify how the dog is created.

So in this case, the constructor is kind of trivial.

The weightInPounds should just be set equal to w.

And so now if I try running DogLauncher,

I run it and I get woof.

So this code is now functioning, because now I've

added this constructor.

If you came from a language like Python,

this is very similar to the init method that classes have.

One little side note I'll make is,

you'll notice I didn't recompile the Dog class.

It turns out that the javac command in Java is very smart.

And if you make a change to Dog and recompile DogLauncher,

it will realize there's a dependency.

DogLauncher needs Dog to work.

And it'll actually also recompile Dog under that.

The Description of [Classes, Video 2b] Defining and Instantianing Classes