Simple Class Example

8 minutes
Share the link to this page
Copied
  Completed
Java Class File. A Java class file is a file containing Java bytecode and having .class extension that can be executed by JVM. A Java class file is created by a Java compiler from .java files as a result of successful compilation.

Transcript

Hello in the Java programming video, we are going to kick start the classes and parts of this series, which is object oriented programming if you were to do more, and there'll be a variety of video covering all of the different facets of classes. So what is a class essentially, you can think of it like a blueprint, a guide of how to create sort of like objects and classes, objects go together, you create a blueprint, which is a class, that you create objects of that class, and it has all of its properties. So if, for example, you have a game, and you have an enemy in there, and you need to have 1000 different enemies, but they're essentially the same. I have lots of different position Maybe a couple of other different parameters, but overall the same enemy having a class called enemy or something along those lines would be fantastic and they could just reuse it again again if you were to make a change to the class all the objects or you know those files or enemies would have the change well, so let's just get to it and create a new class of by default though the class created for the main method, but that was something default that you required and Eclipse luckily did it for us.

You can create another class in you know, the same file recommended that you create a normal file for it. So if you'd right click SRC go to new class, and for this we were going to set a package do so now. And we need to set some goals and certainly all sets and they love animals. Okay, so Okay, is it just, you know, recommend a tweet, no. Okay, that's fine. And leave everything else.

It is click Finish. And there we go, we got this class. So what you want to do is implement, like a very basic version of a class. And the classes has too many variables and methods. So I'm going to create a variable tomorrow what data type it is. It can be any data type, don't say int lawyers, for example.

And then we have flow. And we're gonna have a method called boy and we'll we'll have this I'm actually passing like, damage, damage does implement the method like so, we are going to put minus equal. Okay, so very simple class has been implemented. We go to the Father man wolf fire, the process is very similar, you go to import, we're going to import him module, you put the name of the package dot then the name of the file for enemy semicolon. Now, to create an object of you know that class is really simple. All you have to do is to enemy now I'm going to call it something and I'll call it a one And now it has been created to the E one dot, and we can access enemy can access it Proctor's for the E one dot lawyer equal 10.

So he needs to be public so go back to the public because by default it is not public in Java. My papa will be covering, you know all this stuff about public and you know, it's called scope locks that will cover all of that stuff very soon. Okay, so now if we go back and open simony no Okay, if we go back and need the default values equals health is by default 100 lies by default. Which one? No back sorry, they're not what the hell I'm doing. He won.

He calls No. Okay, so we don't want that we want you want sorry about this. He wanted to enemy that's what we should leave it there we go and we can keep the default value. And then if I was do system dot print ln to print one dot log long lines, see what we get there Get 10 that's pretty cool. Let's create another enemy now, enemy were created before actually will do. Enemy to the new enemy Eat to Live.

That's actually now I'm going to do is chromed out enemy to live until we get 10 one. So this is the beauty of classes are more specifically objects of those classes each object though it has all of the methods and the variable of that class. It has its own instances of you know those properties. So if anyone has a particular variable and is set to somebody, and we too will have a different variable though you might act the same way with us. In the class, but you can store different values that is really really powerful. Now let's have a look at the other episode to the how.

And if we do we are passing nine for example. Now households are meant to say us out into the how and I'm also known printer wants help as well. So I'll print that he wanders into second see printer 191 42. So he independently will allow us to call the reduce health method and reduce the house for whatever sort of code you want inside of the method. Now Luke's condition statements, it does not matter. Don't worry over the next few videos, we'll be covering all of the different features and nuances of classes.

So feel free to stay tuned for those. I look forward to seeing you there and I'll see you very soon.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.