Hire everyone in this theater all I'm going to touch about constructors in Java.
So first when need to create a job or project find new click on Java project and for the project name
just type in here constructors in Java and click on finish.
So as you can see here our Java project is now created.
So now we need a main class for our project.
So just click on this as our See folder here and then click on File New and then click on class.
So for the class name just type in here Main and for the package name we need our reverse domain here.
So just type in here com that your name dot project name constructor's
in Java
OK.
And for the modifiers choose public and here make sure that you check this check box here that says
public static void main method and then click on finish.
So as you can see here our main class is now created.
So now let me actually delete this comment here and then.
Now I want to actually create Tigard objects.
So in order to create a target object I need a class called Tiger in order to actually show me how I
can actually create Piute objects.
So just click on this package here and then click on File New and then click on class OK.
And for the class name just type in here Tiger.
And as you can see it's going to actually put this class inside of this package.
OK and for the modifiers choose publish and click and finish.
So as you can see here are Tiger class is now created.
So now I want to actually specify the characteristics and the behaviors of my tinier objects.
I to actually create some instance variables here.
So that data type of my instance variable is in it and the name of my instance variable is speed.
And then I assign a value to my variable and make sure that you actually put the private access modifier
for your instance variables.
OK.
And here I want to actually create a get here and also turn my head for this instance value.
OK so the access modifier of my get RMF it is public.
And the return type of My guess is in the name of my guess is get speed.
OK.
And then I need to put in a pair of round brackets after my name.
And then I need to open and close my Meffert by using these curly braces.
And then I return the value of this instance variable.
So here I want to create a set here for my instance variable.
You can actually create only a get here for your instance variables.
If you don't want to allow any other classes to change the value of your instance variables.
OK.
For example inside the main class if you want to actually create an object from the Tile class I just
need to type in here Tiger and the name of my object is my tiger I put in an assignment operator here
and there I use this key word here.
New and then again I refer to the name of my class and then I put in a pair of round brackets after
my classmate.
OK.
And then I need to put in a semicolon at the end of my statement.
So now I can actually access the value of my speed instance variable and then output that value to to
the console by using this term.
OK so here I can just type in here.
System that out that princeling.
And then inside the prentices I just need to refer to the name of my object my tiger that gets speed
and then I need to put in a semi-colon at the end of my statement.
So now if I run on my project as you can see this value is the value of this speed instance variable.
OK.
And as you can see I didn't create a search term I felt for my speed instance variable and I can only
access that value in my main class inside the main if all.
I can not change the value of my speed instance value but of does my target object.
OK.
So here I want to actually create a set term I felt for this split instance variable.
So the access modifier of this instance variable is public.
The return type of this method is void.
This means that disbenefit is going to return nothing.
And the name of this method is set speed and that I need to put in a around brackets after my Meffert
name.
And then I want to specify that this method is going to accept an argument.
OK.
So the data type of part of my argument is 8 and the name and the name of my argument is value and then
I need to open and close my method by using these curly braces.
And then I refer to the name of my speed instance variable and then I put in an assignment operator
here and then I refer to the name of my argument value.
Now I want to actually create a behavior for my Tigard objects.
For example of my tired objects can fight.
So the axis modifier of this method is public.
And the return type of this if it is void the name of this method is fight.
And then I need to actually put a pair of round brackets after my Meffert name and then I need to open
and close my if by using these curly braces.
So inside my head I just need to type in a system that out that princeling and that inside Apprentice's
I can just put in two double coats and inside the double coats I can just typing it OK.
And then I need to put in a semicolon at the end of my statement.
And then inside the main class and inside the main method I can just refer to the name of my object
my tiger that fight.
And if I hit on my project as you can see disvalue is now out with it to the console.
OK I want to actually talk about constructors in Java.
OK so what is actually a constructor constructor is a block of code which runs when you use new keyboard
in order to instantiate an object.
OK.
So as you can see here you are actually using this new keyboard in order to instantiate an object from
this tiger class.
OK.
It looks like a method.
A constructor looks like a method.
However it is not a method.
Methods have the return type but constructors don't have any return type how to call a constructor.
The constructor gets called when we create an object of a class.
OK.
So as you can see here in this line of code in line 8 you are creating an an object from this tiger
class.
Ok so here we are calling the constructor actually the default constructor for our tiger class.
Ok so here we are creating an object from the target class and we are calling the default constructor
for our constructor constructs their values.
For example it provides data for the object.
That is why it is known as constructor.
So what are the rules for creating the Java constructor.
There are basically two rules defined for the constructor.
One the first one is constructor name must be same as its class name.
OK.
And the second rule is constructor must have no explicit return type.
OK so now inside the target class you want to create a constructor for this class.
So the first rule is that the constructor name must be the same as its class.
Ok so are the access modifier of my constructor is public.
So it's better to always use the the public access modifier for your constructors because we want to
use this constructor from outside of this target class.
For example inside the main class I want to use this constructor OK.
And the name of my constructor is Tiger.
And then I need to put in a pair of round brackets after my constructor name and then I need to open
and close my constructor by using these curly braces.
So as you can see here this constructor doesn't have any return type so it is not a method.
OK this is called constructor OK.
So inside of this constructor I can actually provide data for my target objects.
OK so what are the types of Java constructors.
There are two types of constructors.
The default constructor which has no argument at all.
OK.
Which is called no argument constructor.
And the second type of constructor in Java is called parametrized constructor.
OK so the Java default constructor or constructors that have no path emitter is known as the default
constructor.
OK so as you can see here this is our default constructor.
OK this type of constructor is called the default constructor.
OK.