Java Programming Tutorial – 04 – Defining a Class and Creating Objects in Java
Video Title: Java Programming Tutorial – 04 – Defining a Class and Creating Objects in Java
Hello welcome to this lesson of mastering Java here we’re in Lesson Four 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 uh 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 and as we go through the future lessons um 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 uh and
Then from that template or that class we can define an object that’s a specific instance a creation of a 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 object for a minivan we can create an object for you know an 18wheeler whatever uh 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 uh 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 um 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 um 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 uh where it says public class lesson 4 up here this is the name of my program lesson 4. Java inside of it is the main method and you all know that uh execution comes into the main method uh
And that’s how we’ve been doing all of our programs so so far but when you create another class uh 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 of that 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 terms 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 um 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 Cur
Closing curly uh there now inside of that class 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 um 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 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 um 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 could have Cruise speed like this so This aircraft might go 150 miles hour might go 300 miles an hour but we know that every single aircraft is going to have a crew speed with it and yes you could Define your crew speed as a double
If you wanted to Define it as you know the top the crew speed in terms of 3505 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 uh it’ll be a decimal value so an aircraft might have 50.5 gallons or 50.7 five gallons fuel capacity that 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 as um almost like miles per gallon in terms of um in terms of what you might have uh 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 F capacity like the tank can hold the cruise speed could be in miles per hour a lot of times
Aircraft miles uh speed is is u in knots which is nautical miles per hour but anyway for this example we’ll just call it miles per hour you could also use kilometers per hour what you could 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 defined the class to exist between these curly braces and everything we are keeping track of in our craft in our aircraft class is how
Many passengers it can hold uh how fast is its cruising speed um how many gallons of fuel can it hold and how fast does it burn that fuel how many gallons per hour does so an air aircraft might uh you know suck down five gallons per hour when you’re flying or 10 gallons
Per hour or something like that so of course aircraft have lots of different characteristics you know you could put in this class you know length of the aircraft wingspan of the aircraft um how many bags can it hold I mean there’s many many things you can put inside of a
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 uh there are no errors there’s nothing underlined anywhere uh inside of here now remember that the purpose of a class is a template it nothing’s actually
Stored in memory right now um because we haven’t created any objects 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 that’s 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 uh a little obvious first before I create this class let’s say or 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 an integer let’s say I wanted to call it there one and I would assign it equal to five and I would put a semicolon right or if I wanted to create a double it’d be double there 2 equals 3.65 something like this we’ve been
Doing this a lot you know we have the type of VAR 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 the class name which is
A template for the type of object uh 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 uh and then we want to give it
A name so let’s say uh this is a very popular airplane airplane that you can go rent at an airport cesna 172 you probably have heard of that all right so this is the type much like integer is kind of like a type this is the name of our object much like ver
One is the name now we have to put something in it right so we do an equal sign um now you don’t just type a value um 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 creates 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 it creates objects is a little bit different than what we’re doing up above but 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 um in memory there’s there is a object that were that is named cesna 172 it is of Cl 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 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 um I wanted to create another one let’s call it aircraft that’s the the type or the the class there and the name of this is a Piper
Saratoga 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 new and what are we creating a new object of type aircraft and you have to put the two uh the two U parentheses 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 or a 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 U 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 of class aircraft that has these variables associated with it and you also have another object called Piper catoga 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 lesson lessons we’re going to learn how to to put numbers into these variables because you know obviously the cess 172 seats four 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 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 run uh the program has run notice how it changed down here there’s no output because we we didn’t put any print statements but there are no errors I just wanted to kind of show you that that that everything’s cool and that the
Uh program is well constructed um let me go ahead and comment this out though this class definition I want to just drill it home I told you that this um this uh object creation is sort of kind of like what we have up here right and
And it kind of is when you think about it because I can do int Json and then in the next line I can do Json is equal to 5 right and I can of course choose to um 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 serat 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 become
Second nature to have the class name and then the name of the object that you want is equal to 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 um 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 it
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 um and then you can instance your own objects there uh in the exercise and then after that following on to the next lesson where
We’ll finally learn how to access these variables that are part of the class and are part of these objects here in Java
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
Oh my goodness, you are amazing brother. After millions and millions of videos, finallllllyyyy I found a person that can teach. 😢 ❤
Thanks for teaching us about airplanes sir!
Thank you! Explained it better than a "beginner" java book AND my college's lecturer.
I just remember this video after years. Now Im Senior Software Engineer. Thank you ❤️
For some reason in VScode its making me put the main at the bottom
a class is a template for our object; a object is where the the computer stores memory in the program. type name = new type()
Thank you!
Yeah i mean OBVIOUSLY the cesna172 seats 4 people.
who doesn’t know that lol
Incredibile !
I just want to write Thank you so much for this awesome video.
I love you
what's the point of making a new class if we can make all of that in same one ?
Please where do we get the exercise for the lesson???
Thankyou
clearly well explained. where are the exercises though?
Thanks a lot
Sir you forgot
Public void aircraft ()
Thanks this really helped me deeply understand objects in java👏
Idk if u still on this but can u do a video with uml diagram translate
“Every aircraft has wings”
– Wrong.
“Every aircraft has a cruise speed”
– Wrong.
“Every aircraft has a fuel tank”
– Wrong, again.
How many wings does a hot-air balloon have? What is its cruise-speed?
How much fuel does a glider carry?
Ahhhh well… For the sake of the example, we can forgive some factual errors, I suppose.
Good video and explanation.
Thank you. I was a great help on us.
Why can't teachers just teach you like you just did here?
nice explication thanks man a bit late but thank you SIR.
THANK YOU!!!!!!! This was explained to me better than the Java Lang textbooks.
wow one of the best tutorials I've seen by far
I WANT TO PROGRAMM AN ATOMIC BOMB
This guy knows his fucking aircrafts yo.
Thank you though bro, this video was extremely helpful for me 🙂
the view is blur
THANK YOU! First year as a CS student. Midterms are coming up. This video was so helpful!!
Thank you so much! Because of this video made our lesson clear to me!❤️
"Now, every kind of aircraft has a fuel capacity"
Gliders would like to have a chat with you 😀
Well explained sir!!! Very informative and easy to understand. Thank you (new aspiring programmer here)
this was incredibly helpful, thank you so much!!! you make it so easy to understand
plane go woosh
Where can I find exercises that Jason was talking about?
So happy I met @Darktrode12 on Telegram he changed my life after being duped on this platform so many times he got my cards delivered to me with great balance on it..So I’m so proud of him and vouch for him
Wow u explained this very easily, the only one who I understood
Class is just a template for making something, called an 'object'.
super helpful video, your explanation made a lot more sense then a lot of places i have been learning from
ohhhhh, wow was overtaken by this classes in java but right now System.out.println("I understand all of them");
Helpful!
I never comment on videos also I am pressed for time because I have an assignment due soon but thank you so much for this content. Sometimes our teachers are so experienced in this field that they go fast because its easy for them. Thanks for dumbing it down this helped me a lot.
was struggling with getting my head around these concepts but this is so helpful and i now understand, thank you
Your one of the best tutors ever, like for real. I wish I had you as my teacher in High-School. no cap
For more visit: https://youtu.be/q2RgFH1rPos
package basic;
import java.io.*;
import java.util.Scanner;
package basic;
import java.io.*;
public class Prog20
{
public static void main(String args[])
{
try
{
System.out.println("————");
System.out.println("0-Heads");
System.out.println("1-Tails");
System.out.println("————");
InputStreamReader in = new
InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
System.out.println("Pick Heads or Tails : ");
Integer x = new Integer(br.readLine());
if(x == 1 || x == 0)
{int coinflip =(int)(Math.random()*2);
System.out.println("fliping the coin……");
if(coinflip == 0)
System.out.println("Heads");
else
System.out.println("Tails ");
if(coinflip > x || coinflip < x )
System.out.println("You lost ");
if(coinflip == x )
System.out.println("You won ");
}
else{
System.out.println("You put in the wrong number");
}
}
catch(Exception ex)
{
}
}
}
simple heads or tails game
After saying ”aircraft piperSaratogs = new aircraft();” can you replace ”new aircraft()” with any thing different? Like maybe just one or a few of the specific variables and / or functions created within the class?