Java Constructor Tutorial - Learn Constructors in Java

Sharing buttons:

if you're confused about constructors do

not fret this video you're going to

learn everything about constructors

constructors are one of those big words

that come with object-oriented

programming the named constructor is

kind of vague and a bit weird so I'm

going to clear up any confusion you have

today about constructors and by the end

of this video you'll know exactly how to

use them in your Java program I hope

you're having a great morning afternoon

or evening my name is Alex I make Java

videos just like this every single week

so if you're new here then consider

subscribe let's hop in Eclipse

go to file new Java projects I'll call

it something like construction for

constructors open it up and then create

a new class inside of the source folder

and we're going to call it shirts that's

the kind of object we'll make today

we'll make a shirt and to see what else

is going on we'll right click on source

again and make a main class which will

have the main method which will let us

run code inside of these curly braces so

we can see what's going on so now we

have main Java and shirt Java the word

constructor is a lot like construction

you see construction workers working on

a building teachers will construct

curriculums a construction worker makes

a building a teacher makes a curriculum

a constructor basically makes an object

now let's recap what objects are

everything in life are objects pretty

much this mouse is an object this

acoustic foam panel is an object which

I'm going to put right behind my camera

in a second my phone is an object

everything so why do we have object

oriented programming well if everything

in the world is an object we can kind of

program everything in the world inside

of a computer

hence object-oriented programming now an

object in real life has certain

properties and can do certain things so

we represent objects in the computer as

having certain properties and doing

certain things so what are the

properties of a shirt well for example

the shirt I'm wearing it has a size and

it has a color so to turn this physical

shirt into an object in the computer

let's add some properties well we can

say it might have the property color so

we can create a variable here called

color and we're adding this private

keyword so that only what's inside of

the object knows what the color is don't

worry too much about that right now if

you're interested in about private and

public keywords they're called Java

access my fires you can learn more about

those in a video on the screen now

but our shirt has a color and it also

has a size so we can create a char or

something sighs those are the properties

of this shirt now what are the things

that this shirt can do well let's say

you can put a shirt on you can take a

shirt off so let's make some methods put

on and I'll just print out shirt is on

and take off take off a trick you can do

with printing out is type sis out and

then control space and that'll finish it

for you pretty cool little trick I use

sometimes so shirt is off so now our

object has properties and things that it

can do let's try to use the properties

and things it can do inside of the main

class and you'll start to see the

importance of constructors so we come

into the main class here to use this

shirt we have to create this shirt so to

create the shirt we type the name shirt

we call it s equals new shirt this is

how you create any object in Java and

since shirt is in the same package

it knows what it is so we don't have to

add an import statement at the top or

anything like that next to use things we

type s dot and this brings up everything

that the shirt can do and we can see put

on and take off so let's put on the

shirt save it and run it and we see

shirt is on because when we click the

green run-button we run code in here we

create a shirt and we call s dot put on

which goes in here and does this so

before we said that constructors make

objects and this line is how you make an

object you say there's going to be a

shirt object called s and it's going to

be equal to a new shirt new shirt makes

a new shirt so if this makes a new shirt

and constructors make new objects then

this should be the constructor and it is

the constructor shirt is the default

constructor which means this is how you

make a shirt by default just like how

the put on method has these parentheses

the constructor also has parentheses

because the constructor is basically a

method that makes your object and all

methods have parentheses after them so

put on has parentheses and the

constructor has parentheses and you

might say well Alex put on is a method

inside of this shirt class I see put on

right here but I don't see shirt where

is the shirt method it's not in here to

make a constructor inside of your class

you just type the name and then finish

it off just like a method so if we print

something out inside of here and then

run it you'll see that when we create

the shirt and call shirt it now thinks

that this is the constructor and so it

runs code inside of there but when this

is gone then we use the default

constructor which doesn't have any code

in it it just makes makes it so this

comes to the question well why would you

ever want to make a constructor if you

already have a default constructor which

makes objects for you well you may want

to create a shirt with say certain

attributes so instead of making a shirt

and then setting the color to white and

then setting the

sighs to medium you could say I want a

shirt that's medium and way all at the

same time so let's make our own

constructor together I actually haven't

used the color and size inside of

methods yet but this would be as easy as

saying set color color taking in a color

and then saying color equals I'll call

this new color new color will change

these to static static and then doing

the same thing for size set size new

size new sighs sighs it should be a

chart so if we use the default

constructor this is what we would have

to do we'd have to say a shirt equals

shirt s dot set color to white and then

s dot set size to M change these to

public real quick so that we can see if

you're following along that's a really

good you will really start to see what's

happening if you're following along here

it'll make a constructor in a second but

I really this is really important that

we do this first so now we can see s

dots color and s dot size save and run

it now we can see white and M which is

what we set through the shirt methods

but this is kind of a lot if you just

want to create a white medium shirt if

you're creating a lot of objects very

quickly and very often then you might

not want to call set color or set size

all the time you may want to just pass

those in as parameters to the

constructor and instead of saying hey

give me a shirt and I'll do it myself

I can just say give me a white medium

shirt so let's do that let's destroy

these and create a constructor so

remember the constructor is just the

name and then treat it like a method so

the name here is shirt you say shirt and

then treat it like a method this is the

default constructor still so we really

don't need to put anything in here we

could even just not have it but this is

here just so you can see it so we can

make another constructor if we want to

pass parameters so we can say shirt

again just like before there's some red

underlines because they're like yo this

is the same code why do you have it

twice well we're gonna pass in some

parameters so we'll say we can pass in a

color you could stop here you could just

pass a color or you could add the size

as well sighs if we save this and run it

instead of using this constructor now we

can use this one just like any method we

see there's two parameters color and

size so we'll just pass in those two

parameters say white and and if we save

it and run it we get null and that's

kind of weird right well we called the

constructor you say a shirt s equals new

shirt with two parameters

well I know there's a constructor with

two parameters here so that's what we'll

use but we're not actually doing

anything with the color and size we're

just passing it and then making an

object we're not setting it so we got it

set so we'll say that color equals the

color and then the size equals the size

and this is where it gets into well the

name here is the same as the name up

here there's a way to get around that

using the this keyword but I'm just

gonna change this to new color and new

size and then replace that here

okay so now if we save it and run it

then we get white and so remember all

the constructor does is it makes an

object for you that's why if you create

like a scanner scan equals new scanner

this scanner I mean if we import it and

everything inside the scanner class just

like inside the shirt class it has a

bunch of different constructors too so

it has a scanner with a file that you

pass into it a constructor scanner with

the input stream all these so you can

pass a path and a char set you could put

a file and a string these are all

constructors for a scanner so just like

we have shirt it has a bunch of these

that pass different parameters inside

all the constructor does is make an

object and you can have as many as you

want and passing different things and

yeah that's that's basically it if this

was helpful please leave a like I've had

a lot of requests to do this video on

constructors in Java so yeah I'll see in

the next video have a great day