Java Programming Tutorial – 05 – Storing and Accessing Instance Variables of an Object
![*](https://i0.wp.com/allprowebdesigns.com/wp-content/uploads/2023/12/1703347659_maxresdefault.jpg?resize=840%2C430&ssl=1)
Video Title: Java Programming Tutorial – 05 – Storing and Accessing Instance Variables of an Object
Hello welcome to this lesson of mastering Java this is lesson number five here we’re going to finally learn how to use the objects that we’ve created and we’re finally starting to get into real object-oriented programming so here all I have done is copy the relevant parts of the last
Lesson so this should all be familiar to you in the last lesson we created this class called aircraft and we said we’re going to use it to create objects that are different types of aircraft so here is a very popular aircraft called Cessna 172 and here’s another popular aircraft
Called Piper Saratoga these definitions here are just creating the objects of these names and we’ve done that in the last section so there’s nothing at all new here it’s just copied over so I can continue with this example now we’ve also said that when we create this
Object called Cessna 172 because it’s a part of the aircraft class that means that associated with this name are all of these variables down here passengers cruise speed fuel capacity fuel burn rate that those are kind of tagged so to speak or associated with in memory this object name and and
Similarly Piper Saratoga is separately in memory an object that also has these variables associated with it that’s kind of the whole point of object-oriented programming right so what we want to do in this lesson is learn how to now assign values for the cessna aircraft into these variables and assign values
To the Piper Saratoga aircraft for these for these things and also learn how to print these values to the screen and so we’re going to be really using the object-oriented nature of Java to do this it’s very very simple once you have the object created all you have to do is
Just treat it like a regular variable but you have to tell it what object you’re talking about so Cessna 172 and we’re going to use the dot notation so this is the object and whenever I press the dot notice how this dialog pops up and it’s telling me that cruise speed
Fuel burn rate fuel capacity and passengers notice how they’re a different color here and they’re all up here at the top and notice how it says aircraft aircraft aircraft aircraft that means that Java knows that this class exists here it knows that aircraft exists and it knows that Cessna 172 is
An object of that class so when you put the dot here after the name it giving you suggestions which one of these variables do you want to use now you can either click one or you can just keep typing for instance if I ignore it and I just kept typing I can type
Passengers just like that notice I’m not using the dialog box at all and I could set it equal to four because a Cessna 172 carries four passengers now notice that whenever I hit the semicolon everything disappears the variable changes to blue to match the blue down here and everything’s fine there are no
Errors here in fact if I save it and I run it everything’s run with no errors but of course I haven’t output anything to the screen yet so let me go down below and say well let’s assign something to the cruise speed variable for a Cessna 172 so Cessna 172 I hit the
Dot and whenever I do that again this dialog box pops up so I can go down here to cruise B which is already highlighted and I can hit enter when I hit enter cruise speed automatically pops in there so it’s a time saver equals and in this
Case the cruise speed for a Cessna 172 is a hundred and forty miles an hour roughly so that’s what you put in there for the cruise speed of a 172 and then thirdly Cessna I think you can see the pattern here 172 I hit my dot and I can
Go down to fuel capacity I can go down see how I’m going up and down in this list with my keyboard and I can hit enter fuel capacity and I can say a Cessna 172 carries 56.5 gallons of fuel with a semicolon and then Cessna 172 dot fuel notice how as I continue
Typing burn it eliminates everything else and I can either hit enter to continue or I can just keep typing the fuel burn rate for a Cessna 172 is 9.5 gallons per hour so this is in gallons per hour this is in gallons this is in
Miles per hour and this is the number of passengers so now we have assigned values specifically there for assistant 172 as they associate to these variable names now it’s important for you to realize that these variables that are part of the object that we call Cessna 172 they’re they’re just variables like
Anything else I mean I can I can take this variable and I can multiply it by three I can divide it I can use mathematical operations I can also use it to print things to the screen just like we can print any variable to the screen so for instance if I want to
Print to the screen I can use this print statement like we’ve been done let’s do let’s say so let’s say something like Cessna 172 passengers colon space and then over here I can do plus and I would put any variable name but I want to print out to the screen how many
Passengers that can hold so I can say Cessna 172 dot and again this is gonna pop up and I can just go down to passengers or I could keep typing and that is a variable this whole thing with the dot is a variable it references the variable that’s inside of this object
Here notice I’ll tell you right now though notice how we’ve been using all along system dot out dot print line this is a method that is part of this that is part of this so it’s a hierarchical kind of thing the passengers is part of the object Cessna 172 so the dot notation
You’ve actually been using before but now you can kind of understand the reason why it’s there it’s because everything is an object in Java so we’re going to hit save and I’ll run this guy Cessna 172 passengers : 4 and when it prints this out is using the value that
We stuck in there we can print anything we want out system yep let’s do system dot out dot print Ln and let’s go in here Cessna 172 fuel burn rate and then over here I can put plus Cessna 172 dot fuel burn rate and then I can continue
Here just like I can always do with the print statements and I can say gallons per out gph gallons per hour so let me go ahead and do this and hit print Cessna 172 passengers for Cessna 172 fuel burn rate 9.5 gallons per hour so I’m just showing you here how after
You’ve created the object you have access to all the variables that are a member of that of that now of that object because whenever you create the object you’re setting aside memory to hold all of these values now similarly let me go down here similarly we actually created two
Objects or earlier we created a Cessna 172 which we’ve done here and we’ve created a Piper Saratoga that’s a different kind of aircraft that’s a little bit faster so what we can do to assign values there is we can say Piper Saratoga dot and when we do that again
Java knows that these and these variables are part of that object now because it’s and they’re created from the same class so let’s go ahead and select passengers double-click passengers like that Piper Saratoga passengers can hold six let’s go a little bit faster cruise speed I’ll hit
Enter the cruise speed for that airplane 201 miles per hour Piper Sarah toga dot let’s do fuel capacity I’ll hit enter to complete it and that’s a hundred and two point five notice that these are decimals here because the fuel capacity in our class is listed as decimals and if you’ll burn
Rate is the stood as a decimal as well we’re gonna hit semicolon there and then the last one Piper Sarah toga dot let’s do fuel burn rate that is twenty point five gallons per hour so I’m assigning to that variable so notice that each of these objects the Cessna and the piper
Contain the same variables in each case and you reference it with the dot the variables that are defined and the types are all contained in the template that we call a class now if we wanted to print something out here system dot out dot println we could say
Something like Piper Sarah toga cruise speed and notice it’s underlying it so we need to have quotations that’s what’s the problem there so we’ll go ahead and close that off and then over here we will have Piper Sarah toga dot crew speed right and then I can put another
Plus five wants to this is optional miles per hour because that’s how I’m measuring the speed so let me go and do that and let me go ahead and do a back slash into space this down a little bit so we don’t get confused and we’ll hit run Piper Saratoga
Speed 201 miles per hour notice how the 201 and the miles per hour are on top of each other because I didn’t put a space here if I put a space for the miles per hour then I’ll run it again and you can see 201 miles per hour which matches
Exactly what we typed in for that variable name so it’s important for you to know that these variables that are part of an object there they can be used just like any other variable I can do computations with them I can multiply this by five or divide it or whatever I
Can use them in print statements I can use the math methods that we’ve talked about like square roots and cube roots and things like that I can these are just regular variables it’s just that they’re kind of aligned in a structure that we’ve defined which is the aircraft
Class I hope you can see now how classes can be useful because I can define this one place and as a programmer or someone that’s reading code I can come in here and see what does this class look like oh it contains this stuff and then I’ll
Know that any time I create an object of that class I’ll have access to all these variables and so on and later on we’ll learn that this class and I’ve kind of hinted before this class can contain variables which I’ve already kind of shown you how to do here and it can also
Contain methods to operate on these variables maybe I want to do some computation some aircraft computations dealing with fuel capacity or whatever and I can define methods in here to do that we’re gonna all get to that in a little bit but for now just know how to
Create your class how to create the object from the class and how to access the variables with the period notation as we have done here now I encourage you to go off to the exercises do the exercises the exercise that I have in there because I’ll give you a lot of
Practice with doing this with a different a different class and different objects often the exercise so go do that and then follow me on to the next lesson 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
You have been the best instructor of programming I've seen so far. Your explanations are simple and put in such a way anyone can grasp the concept of java programming!
He really explains everything well
you're amazing i love you thank you
this man can teach
I am learning java in a bootcamp, and today's class didnt make much sense, try to watched couple of videos and ended up confused as $hit! this guy is a freakin legend! he made the whole thing so easy and crystal clear to understand, if you dont understand what is he talking about then Java is not for you.
thanks for the awesome video, bloody informative and definitely helped me big time.
Will you please help me,how to develop a dictionary using java
Great Video,simply explained.Good Job
hey men u saved hours of headache, thanks for this
Thank you SOOO MUCH Mr. Gibson! You're a WORLD-CLASS teacher! I've learned so much from you, it's absolutely amazing that I can now write code very EASILY using classes, objects, & methods passing arguments through the parameters. All of this was so difficult to learn prior to discovering your brilliant training courses. Again, I'd like to thank you so much and I will definitely be buying up your courses. Please keep your awesome training videos coming.
Where can i find the exercise please
Commented before but had to say again thank you. Wouldve failed without you
Where are the exercises
he asked to do
Excellent, as always.
Great tutorial! Where can i find the exercises?
Do you need to create the new object in main?
I could comment but I'd be just an iteration of all comments before me. You cleared things up for me in just 6 short lessons that I watched in an evening, that previously have been confused about in the past 12 week classes on OOP.. Thanks so very much, I was getting really discouraged that I was an old dog Fortran programmer stuck in the past…lol
You Saved My Hole Life, Thank You !
For more visit: https://youtu.be/wbrWpt–cgw
Honestly man your way of teaching is incredible I hope you read this massage , and because of you am passing the java class with an A .
Thank you
This is god tier. Idk if I understand how objects work 100% or whatever this was. But it makes much more sense god bless u sir
love you soooooooo much !!!
you are the best …you cleared a lot of storm from my head
Should we type "static" before we define a class aircraft, otherwise it will pop up error Error:(5, 32) java: non-static variable this cannot be referenced from a static context
These videos are fantastic. Thank you!
Microsoft Flight Sim is coded in Java, Colorized, circa 2008.