declare

Java Programming Tutorial - 04 - Defining a Class and Creating Objects in Java



Sharing buttons:

hello welcome to this lesson of

mastering Java here we're in lesson 4

we're going to begin to talk about

classes and objects again but now we're

going to actually write the code to show

you how it's done and I want you to

think back to the last lesson that we

just learned when I tried to tell you

that a class definition in Java is just

a template for making something the

thing that you make is what we call an

object so think back to that example and

keep it in your mind as we go through

this lesson as we go through the future

lessons the example we used in the

slides was a class called vehicle and it

defined things like the color of the

vehicle and the top speed of the vehicle

and the number of passengers in the

vehicle and things like that and then

from that template or that class we can

define an object that's a specific

instance a creation of something in

memory that is going to use that

template so we could create an object

for a Corvette and populate those

variables with specific information we

can create an object for pickup truck we

can create an object for a minivan we

can create an object for you know an

18-wheeler whatever those are all of

class vehicle or of type vehicle so

we're not going to create a vehicle

class here we're going to change it up a

little bit and create just for purpose

of illustrating something we're going to

create an aircraft class which is again

something that's a general category you

know all aircraft have wings they have

some way to propel themselves through

the sky aircraft have certain speed they

have certain fuel capacity and things

like that so we're going to create a

class called aircraft and then we're

going to use that class to define some

objects of specific types of aircraft

that that exist so in order to create a

class notice that this is the the blank

template when we create any new program

in Java here in the Eclipse editor where

it says public class lesson four up here

this is the name of my program lesson

for Java inside of it is the main method

and you all know that execution comes

into the main method and that's how

we've been doing all of our programs so

far but when you create another class

you're defining out something outside of

everything that we've done here so you

need to go down to the bottom curly

brace notice that this opening curly

goes with this one so you need to go

outside

to define your class because it's

existing outside of this and then we

will go from there so what you do to

create a class is you use the class

keyword notice that when I type the word

class it turns purple and then I have to

create a name and so in the slides we

were dealing with vehicle that was the

name of our class here

we're going to create an aircraft class

so I'm going to name it aircraft in fact

I'll just make it a lowercase a aircraft

and I'll put a space and I'll put an

opening curly brace and when I hit enter

Eclipse automatically puts the closing

curly brace for me so notice that

nothing is underlined there are no

problems here Java knows that I'm

creating a new class called aircraft

have an opening curly and associated

curly closing curly there now inside of

that class is my opportunity to define

any variables any member variables for

this class and later on in subsequent

lessons down the road we'll also put

methods inside of this class that can

operate on the data but for now we're

just going to be putting characteristics

of an aircraft there's lots of

characteristics of an aircraft for this

we're going to talk about how many

people can this aircraft hold that's

always going to be an integer number of

people so I'll call it integer

passengers with a semicolon there notice

I'm not putting any number I'm not

assigning a number to it because that's

not the purpose of this this is a

template we're not going to assign any

numbers until we define an object that's

a specific type of aircraft what else

could we have inside of an aircraft

class we can have cruise speed like this

so this aircraft might go 150 miles an

hour might go 300 miles an hour but we

know that every single aircraft is going

to have a cruise speed with it and yes

you could define your cruise speed as a

double if you wanted to define it as you

know the top the cruise speed in terms

of three hundred and fifty point five

but most times when you look up an

aircraft speed in a book it's going to

be a whole number so let's just keep it

as an integer now also every aircraft

has a fuel capacity has a fuel tank and

we want to describe that as a double

because usually when you look that up

it'll be a decimal value so an aircraft

might have 50

point five gallons or fifty point seven

five gallons of fuel capacity it can

store on board so we associate that with

a double and then also every aircraft

might have something called a fuel burn

rate you can think of this is almost

like miles per gallon in terms of in

terms of what you might have seen for a

car well an aircraft burns fuel in terms

of gallons per hour so I'll just put a

little note here gallons per hour right

and then this over here will be gallons

that's the fuel capacity if the tank can

hold the cruise speed could be in miles

per hour a lot of times aircraft miles a

speed is in knots which is nautical

miles per hour but anyway for this

example we'll just call it miles per

hour you can also use kilometers per

hour what you can use any measurement

you want but it's still going to be a

speed and this is just going to be a

number of people because that's how many

people the aircraft can hold so we have

created a class we're naming it aircraft

we've defined the class to exist between

these curly braces and everything we're

keeping track of an R crap in our

aircraft class is how many passengers it

can hold how fast is its cruising speed

how many gallons of fuel can it hold and

how fast does it burn that fuel how many

gallons per hour so an air prep aircraft

might you know suck down five gallons

per hour when you're flying or ten

gallons per hour or something like that

so of course aircraft have lots of

different characteristics you know you

can put in this class you know length of

the aircraft wingspan of the aircraft

how many bags can it hold I mean there's

many many things you can put inside of

an aircraft class but for the purpose of

this example we're going to to just

leave it alone so let's go and save it

there are no errors there's nothing

underlined anywhere inside of here now

remember that the purpose of a class is

a template nothing's actually stored in

memory right now because we haven't

created any object an object is sort of

like a physical thing as much as a

computer can hold physical things you

have to realize but a class is just the

template when we associate it with an

actual object

when memory is allocated and that's when

memory is assigned to hold whatever

these variables are going to hold and

the way you do that is you have to

create the object so there's two parts

of this you have to first have a class

definition and then you create your

object so you go up here into the main

method because that's where program

execution is going to start now let me

point out to you something a little

obvious first before I create this class

let's say all right before I create this

object let's say I wanted to create an

integer variable like we've done many

times before I would type in integer

let's say I wanted to call it there 1

and I would assign it equal to 5 and I

would put a semicolon right or if I

wanted to create a double it'd be double

there two equals three point six five

something like this we've been doing

this a lot you know we have the type of

variable we're creating the name of the

variable equals and then the value of

the variable that's something we've been

doing all along right now it's very

similar when you create an object see

here we have the class name which is the

template for the type of object that we

want to create so much like we have to

type integer we have to tell Java what

type we're trying to create so we type

aircraft right so we have aircraft and

then we want to give it a name so let's

say it's a very popular air-crane

airplane that you can go rent at an

airport Cessna 172 you probably have

heard of that alright so this is the

type much like integers kind of like a

type this is the name of our object much

like ver1 is the name now we have to put

something in it right so we do an equal

sign now you don't just type a value so

because this is actually an object we're

creating so you use the new keyword

aircraft and then you have to put the

two parentheses there and hit a

semicolon now I want you to make sure

you understand I'll put a little note

here create object of class aircraft

this is very very important for you to

understand this line because you're

going to see it a lot in Java

programming basically it's exactly the

same now I shouldn't say exactly the way

it creates objects is a little bit

different than what we're doing up above

conceptually you can think of it this

way integer is the type there is the

name five is the value that goes into

this this is the class this is the name

and inside of this placeholder we're

putting a new object of type aircraft

that's what we're doing so at this point

and only at this point in memory there's

there is a object that were that is

named Cessna 172 it is of Klatt has the

characteristics of the aircraft class so

now associated with this name is a

variable that holds passengers a

variable that holds the crew speed a

variable that holds the fuel capacity

and a variable that holds the fuel burn

rate all four of these variables are now

associated with this name because this

is an object and because we've declared

what this class looks like all right now

let's say I actually own two airplanes

which I don't but if I did I wanted to

create another one let's call it

aircraft that's the the type or the

class there and the name of this is a

piper sera toga again another very

popular aircraft that seats more people

and it goes a little bit faster so

that's the type in the name set it equal

to nu and what are we creating a new

object of type aircraft and you have to

put the two the two parenthesis here

we'll talk about why the parentheses are

there a little bit later you can kind of

think of it almost like a function call

that comes down here or an or method

call that comes down here and just kind

of starts this process off of creating

this object but we'll get to it a little

bit later the actual details here but

for now just notice that you have name

or I should say you have type or class

and then you have name and then inside

of this you're assigning a new object

that's created so at this point in time

you have in memory an object called

Cessna 172 that is a class aircraft that

has these variables associated with it

and you also have another object called

Piper Saratoga which is a totally

different aircraft but it's assigned the

same variables because it's a part of

the same class so these two objects now

exist in memory and in future lessons

we're going to learn how to to put

numbers into these variables because you

know obviously the Cessna 172 seats for

people

all right and the Piper Saratoga seats

six people so and of course the speed is

different from a Cessna to the piper and

so we're going to learn how to us to

actually assign values to the member

variables to the instance variables that

are defined down there now I want to say

one more thing

before I move on so firstly go ahead and

run this just to show you that when you

hit and run the program is run notice

how it changed down here there's no

output because we didn't put any print

statements but there are no errors I

just wanted to kind of show you that

everything's cool and that the program

is well constructed let me go ahead and

comment this out though this class

definition I want to just drill it home

I told you that this this object

creation is sort of kind of like what we

have up here right and it kind of is

when you think about it because I can do

int Jason and then in the next line I

can do jason is equal to five right and

I can of course choose to put all this

in one spot and put it all in one line

integer name equals whatever but I can

also split it up into two lines like

this in much the same way you could do

the same thing with this object

definition you could say aircraft hyper

sarah toga right

that is just assigning the name to the

class right just like we just said a

minute ago and then in the next line you

could say piper sarah toga and you got

to put something inside of it new

aircraft like this this is really

accomplishing the same thing as what

I've commented out right here if I hit

save and I hit run I don't have any

errors everything's created just fine so

if you really want to put it like this

you can you're very rarely going to see

that in Java most of the time you're

going to see it like this it becomes

second nature to have the class name and

then the name of the object that you

want is equal to a new instance of that

class so you're allocating this in

memory you're putting it to this name

now both of these guys exist in memory

so make sure you go off and do the

exercise for this lesson because I'm

constructing the exercises to give you

practice you can look at this and

understand it and really feel like you

got it but you don't really understand

anything until you go off and do

yourself so go off to the exercise and

you'll see I have a different kind of

class that I want you to create with

different variables and and then you can

instance your own objects there in the

exercise and then after that follow me

onto the next lesson we will finally

learn how to access these variables that

are part of the class and are part of

these objects here in Java