Java Tutorial for Beginners #16 - Creating Classes
Skills:
LLM Foundations60%
Key Takeaways
This Java tutorial for beginners covers creating classes, constructors, private vs public methods, and get and set methods, providing a foundation for object-oriented programming in Java. The tutorial uses a class called Dog as an example to demonstrate these concepts.
Full Transcript
so welcome back to another Java programming tutorial so in today's video we're going to be going over classes when we talk about creating classes creating some methods constructor methods all that fun stuff creating instances and objects and yeah so this is going to be kind of an advanced video don't if you've been following along so far you'll definitely be able to follow along with it but just know that now we're kind of getting into some harder aspects of Java so if you guys don't understand this please don't hesitate to join my discord server ask me some questions leave a comment down below because this is absolutely fundamental and you have to understand this before you can really start doing any serious programming and that goes for kind of all languages but Java especially since it is an object-oriented language we need to understand classes and how to create classes so without further ado let's get started so so far we've been working in this class called main now this is not really a true class because this class all it's doing for us is just running some code right away when we run the program and that's why we have this public static void main function that automatically triggers whenever we click this little green Run button okay so what I'm gonna do now and you can see that I have some methods that I've added in here that we just kind of use within here earlier okay what I'm gonna do now is I'm going to create my own class from scripts from scratch and we're gonna start coding so to do this we're gonna go to whatever our package is so in this case tutorial 1 for me and I'm gonna go new and class and I just did that by right clicking and now I'm gonna give my class a name now for this example I'm going to create a dog class and yeah you guys can create whatever you want but I would recommend you follow along with me since you guys are most likely new to classes ok so now we have this thing and this says public class dog you can see we open up in a new file so whenever we create a new class in Java we have to actually create a new file for that now it's actually good because it makes it really easy to navigate between different classes whereas in something like Python you can just have all your classes on one file ok so what we're gonna do now is we have this public class dog so what does this what does this mean like what is a class well a class is pretty much a data type and whenever we create an instance of a class like an object of a certain type all we're doing is we're just using all the information within the class to like create an object so it you can almost think of it as like a blooper for an object now what problems do classes solve for us like why are they useful so I'm gonna introduce kind of a very simple problem alright so I want to create five dogs okay I want to have five dogs I want each dog to have a name I want each dog to have an age and I want to at some point be able to like call something and print out the each dog's name and age in like a nice form okay now we could do that we know how to do that if we went back into main here what we could do in this little what-do-you-call-it method here is we could just type a bunch of hints we can say int like dog 1 and this is equal to 4 and that's dog ones age and you say int dog 2 and that's it good to 5 we could go on and we could create 10 variables 5 for the age and 5 for the the names right and then we could go and we could print out each one each time but that is incredibly inefficient and what if I wanted to have like 10,000 dogs well what would I do then well we could use like lists to create or lists or arrays to create names and ages that's just not efficient it doesn't look as good in our actual coding so what we're going to do is going to use a class and you'll see how we can kind of accomplish this problem so within classes we have methods and we have attributes okay now attributes are kind of like variables that hold information for us so in this case we want to have two attributes in our dog class and these are going to be the name and the age to create our attributes that's the first thing we typically do when we create a class is right at the top of our class we're gonna type the keyword private and then we're gonna give a data types in this case me to say string and then the name of our attributes in this case name okay and this is all we have to do we're just declaring that at some point in time we're gonna have the attribute name and it's going to be storing some information later on in our program okay we can also do private int and in this case we'll do age and this is gonna obviously represent the dog's age now if you want to have some other attributes we've it again type private and we can keep going and type a bunch of attributes now what is this private keyword and why do we have public up here and private here well what this private keyword does is it ensures that this name and this age are only accessible within this class meaning that if I tried to do something over here and I wanted to use this age and this name from this class I wouldn't be allowed to do that and the program would say no this is a private attribute you can't access that why do we do that well you'd find out in larger programs but we can also create public attributes as well that are accessible to the other classes okay so if I wanted my things to be public I can put public for now whenever we're using attributes we're gonna use private and it's best practice to use more private things okay and the same goes for methods which you'll see in a minute okay so now I need to create something called a constructor method now this you typically only create one of these although you can create multiple and what this is going to do is it is gonna run automatically whenever we call this dog class and the way we create this constructor and we typically need one of these when we have a class is we're gonna do public and we're just going to type the name of our class once again so that's giving us a public dog like this okay and you can see that now we're getting no errors everything's fine and in here we are going to type the parameters or the information that we need to be passed in whenever we're creating a dog object now in some instances you may have nothing in here when you create a dog object all you need to do is just say you're creating a dog object you don't need to give any information and that's fine but in our case we want to be able to create an age and a name with our dog so what I need to do here is I need to type string name and int age okay just meaning that whenever we create a dog object we need to give it a name and we need to give it an age now what I'm gonna do is I'm gonna set these values so these attributes equal to whatever we pass in okay so what I'm gonna do is I'm actually just gonna use a keyword and it's the keyword is this it's kind of hard if you weren't watching you'd think I'm just like saying this is the keyword in pointing something but this like actually typed out and then I'm doing this dot name is gonna be equal to name and this dot age is going to be equal to H so what is this this this keyword actually doing well it is referencing the attributes of the class okay so when we type this it is going to be looking up here to find all of our like private attributes and in this case we have an age and we have a name all right and that's how we reference things that are a part of the specific instance we have this name and this age okay and I'm gonna explain more and more of this as we keep going it's hard to do it in like small steps okay so now that I have this constructor method created we can go on to create one more method and then we can actually start using this class really simply okay so what I'm gonna do now is I'm gonna create another method and this case I'm going to type a public I'm not gonna return anything so I'm gonna use void okay and the name is gonna be speak and what this is gonna do is it is simply going to say something or prints me up to the screens let me say system dot out dot print LM why did that okay println and all we're gonna do is gonna say I am and I want to say that the dog's name so in this case we'll say this dot name okay and we'll say plus and I am you know a plus we'll say this dot age plus years old okay so it's simply gonna say like I am whatever the name is and I am however many years old not great grammar but that's fine so now how do we actually create a instance of this or how do we use this dog class well from our main class here which we should still have open and you know we can delete all these we don't need all that so we'll get rid of all this stuff here we can actually create a object and to do that of type dog what we're simply gonna do is you can type dog and we're gonna give it a name in this case I'm going to say Tim is equal to new dog okay and then remember that we have to give dog some parameters right so or arguments because we have the name and we have the age so what we need to do is we need to give it a name in an age in this case we tighten up Tim and I'm gonna type for and now you can see no red line we're looking good so our dog's name is Tim and it's age as for okay so that's great let's run the program make sure this is working everything's fine now what I want to do is I want to use that speak method so how can I use that well what I'm gonna do is I'm gonna type Tim which is the name my dog or like the variable for it dot speak like this okay semicolon and let's see what happens we get I am Tim and excuse me I am four years old so the way that this worked right is we created a instance of the dog class and the instance was named Tim and it referenced a dog object okay so now when we created that instance we said okay so this instance this specific one is gonna have an age of four and a name of Tim all right so it's stored that information up here in our private string and our private int and then later on when we decided we wanted to call this well it said okay we'll give me the instance you're calling it on so in this case we're calling it on Tim we're gonna say okay so Tim what's Tim's name well it's name is well it's Tim okay so we'll say Tim what's its age it's H that's four so we'll print four to the screen now we can obviously create multiple dog objects so what's copyist and let's create a few more he'll create three dog objects and I say this is Bill and he is seven and we'll say this is Bob and he is 11 okay let's copy this and I'll keep naming the same thing let's go Bob and let's go bill and then we can simply call the speak methods on them okay so we'll say Bill and we'll say Bob all right and just to prove something I'm gonna say Tim not speak again and we'll talk about why I do that in just a second okay so let's run this let me say I am Tim and I'm four years old I'm Bill I'm seven then go on and you can read through them okay now notice when we call it Tim again it still retains its age and its name when we do this we're not actually changing like this is not one variable we actually now have three different names right we have Tim we have Bill we have Bob and they're specific to each of these variables that are storing that dog object okay so that means that we can hold like unique values for each of our different instances and we can have like infinite amount of instances of a class okay all right so that is about it I think I'm gonna talk about quickly I already talked to a private versus public well let's create some more methods in here and see what they can do and I'll talk about a bit more about constructors because we're only at like 11 minutes so what I'm gonna do now is I'm going to create another method I'm going to call this get age okay so I'm gonna say public and in this case we'll say int gets age all right and all we're gonna do in here is simply return the age to let's see a return statement to wherever we're calling it from so this case I'm going to say is I'm gonna return this dot H now the reason I need to do this is because say I want to get the age of one of my dogs right like say I created it I changed it around I don't know what it is I want the age well we can't simply do something like in other languages we'd be able to do like Tim age right now you see when we do this we get these old red lines and it says the field dog age is not visible and that is because again it's private so it's not letting us see it from over here in this main function only within this muttering method only within this class can we actually reference this variable right so to get the age what we can do is we can call Tim dot get aged like that okay and we can print that to the screen we say like int x equals gonna say system.out.println and o x is already there great so now if we run this we can see that we get four down here excuse me at the at the bottom of the screen now we can actually do the same thing with like setting the age so say we wanted to change the age at some point maybe Tim got a year older well what we would do is we have to create another method in here in this case we're gonna make it public again we're gonna say public and we don't need into this time because we're just gonna change something we're not gonna return something to say public void and we'll say set age okay and then we're gonna take an age because we need to know what we're gonna set it to and we'll just say this dot age equals age so just like we've done up here it's the exact same thing except we're just going to do it within the method set age so now if I want to change the age so let's say instead of that will say set age and we'll give it a value of ten then we print out let's just say Tim not speak again what's gonna happen now sorry guys I'm a bit sick if I keep coughing all over the place what I'm gonna do is Oh what's her air Tim not set age ten return type of set age two int mmm one second here sorry public let's try this in set H public void I know I'm making a mistake over here oh okay that's why I can't do I can't set equal to a variable I have to call like Tim dots at age by bad on that guy's we do speak we can see now it says I am Tim and I am 10 years old and we've changed from the age of four so that's how we would go about changing these attributes later on within the the class okay now I want to do one last one I want to create a private method and show you what this is okay so I'm gonna create a privates Boyd actually now let's do it okay and we're gonna call this ad - it's not gonna make any sense with the dog class but just it's just for an example okay and in here we're gonna take actually we won't take anything but we will get a return we're gonna return this start age plus two okay now this is a private method meaning that it can only be used and seen within this dog class so say I wanted to use this ad - maybe I could do this I can do ad - here okay and just do a little semicolon and there we go I could use add to and that that works fine there's no issues for that okay and that's how you use methods with inside of the class so like speak I can easily every time we initiate the dog call the speak method as well and to do it within the class you don't need to do like something dot you just call the actual name because it's really visible within here right now watch what happens if I go over here and I try to use that so add - so I'll say Tim dot add - like this and you can see that again we're getting a red line and says this is not visible you can't use it because it's a private method okay so I think that's gonna wrap it up for this video on classes hopefully you guys now understand kind of how you can create classes how to create methods somewhat and in the next video we're going to go more in advance we're gonna talk about inheritance we're gonna talk about multiple constructors and then we're just gonna keep on going again more and more advanced as we go so as always if you guys enjoyed the video please make sure you leave a like and subscribe and I will see you again in another word [Music]
Original Description
In this java tutorial for beginners I explain how to create classes. I talk about constructors, private vs public and many other OOP topics.
Java beginners programming tutorial. A complete java tutorial meant for absolute beginners. Absolutely no programming experience required. If you are new to programming and want to get started you are in the right place!
**************************************************************
One-Time Donations: https://goo.gl/pbCE9J
Support the Channel: https://www.patreon.com/techwithtim
Join my discord server: https://discord.gg/pr2k55t
**************************************************************
Please leave a LIKE and SUBSCRIBE for more content!
Tags:
- Tech With Tim
- Tech With Tim Java
- Java tutorials
- Java for beginners
- Java Tutorial for Beginners
- Beginner Java Tutorial
- Java classes
- Java classes and methods
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: LLM Foundations
View skill →
🎓
Tutor Explanation
DeepCamp AI