Java Classes - How To Use Classes in Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
we use classes in every single video but we haven't talked about the actual class itself I want to first say that I've been getting a lot of comments asking me how to think like a programmer so what I did to help you out here as I put together a little guide it's called 7 tips on how to think like a programmer it's got lots of good information in it here contains a method to how to understand code even if you don't have any prior experience it's got tips on what the best programmers in the world think so if you want this 7 tips on how to think like a programmer and just click the link in the description and if you're new here my name is Alex I post a Java tutorial on this channel every single week for you to understand Java and help you out with your classes so if you might be interested in that then please consider subscribing so let's just start it off by going to file new Java project we'll call it our our project hit finish and then open that up on the source folder go to new class this creates the class we want and I'm gonna call it class 1 and hit finish this generates some key words to start letting us make a class a class is a blueprint for an object in objects let you do things in Java you make a database object to get information from a database update information in a database Facebook has an account object that has a user name date of birth gender objects and computers let you do things with them in a class lets you make objects so without classes there wouldn't be objects so let's start building our class literally all this in a class is things it knows and things it can do aka variables and methods let's start with variables let's give class 1 an integer variable called X say it's equal to 2 now this class knows the variable 2 and to test this out what we can do is make another class with a main method goes to run code you got to have a main method somewhere and we don't want to clutter this one right now so we'll just make a new one we'll go to new class this one will be called main and click the public static void main this adds this which runs code when we the green run-button in here so what we can do to test what class one knows is make the object from it so to make an object from a class you type the name name it something and say equals new that name with parentheses the parentheses makes it a constructor which means it makes the object now if we print out C dot now we see X C X whenever you put object dot something it shows you all the things it knows and things it can do variables up here methods down here so it knows X so if we save and run it and then we print C dot X we'll just - you can have all types of variables in here so you could have say an array of character variables if you want it and use it just the same C dot chars beautiful so just with these two lines of code we've created a class that can now create objects and use what it knows and what it can do let's delete this chars because it's kind of getting messy over here change that back you can make multiple objects of the same class too so we could say class one name is something else and now we've got two objects from the same class and now a Dino's x2 so if we ran this we'd get to two we create an object C from the class one blueprint we create an object D from the class one blueprint and then we print C's X and DS X so this one class can make multiple objects from it and that's the real difference between classes and objects objects are made from the classes so now that we've shown what the class knows let's do something that the class can do with a method we'll make a simple print method you won't return anything so we're going to type void here print hi and then we'll just print out hi now class one knows print hi so if we did see dot we can do print hi no salute this C dot print hi you can also do D dot print hi but let's just do one now we print hi from the C object from the class one class that way you can also have multiple classes so let's make another one we'll call it class to know main method because we already got one say class two has integer y equals three now we can do class two C equals new class to print out C dot Y this will print out what Y is which is three this is really the basics of classes and everything else all the other keywords you see the weird logic and stuff that's all extra so if you see like public static boolean is valid with weird parameters like this this is just a method with keywords if you see things like public final int Z equals that like these are all keywords and you learn what the keywords are public means it has access to other files final means it doesn't change type integer but it all boils down to the basics variables and methods inside of a class that objects know when you make an object with the class so that's really the basics I don't want to go further into this because that's really the concept here the class can make multiple objects that will all know the same variables and methods there are concepts of super classes and subclasses using object-oriented programming terms but just really understand what the first super simple super basic variables and methods hope this was helpful remember to get my seven tips on how to think like a programmer I really think that'll help you out trust me I've been through the same thing in my computer science degree so I know what you're going through click the link in the description if you want it I hope you have a great rest of your day and I'll see you in the next video [Music]
Info
Channel: Alex Lee
Views: 135,300
Rating: 4.9360728 out of 5
Keywords: java classes, java class, class java, classes in java, java class tutorial, java class example, class in java tutorial, how to make a class in java, alex lee, java
Id: vjjjGkXpX_I
Channel Id: undefined
Length: 7min 20sec (440 seconds)
Published: Thu Dec 12 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.