hello friends and welcome back in this lecture we will talk about constants and Java here is our outline we will talk about constants we will see how we can ensure us a constant and finally we will see the benefits of using a constant so let's get started first of all what is a constant it is a variable whose value cannot be changed so as we saw previously we can change the value of a variable by simply assigning it to a new value right so what if you want a variable with a constant value so that the value cannot be changed then you will use a constant so first of all to define a constant we use the final keyword as you will see an ultimate also a constant can be used like any other variable now in order to name constants we write them in uppercase and also we use the snake case convention so the name of the constant will be all in appliques and between each word we will put an underscore alright of course this is optional but this is how constants are named in Java and finally you will get a syntax error if you try to change the value of a constant all right so let's see how we can create a constant so as we said we use the final keyword and then we initialize a variable normally so let's see this example we are saying final string company name is equal to this string names are Academy and after that we are printing company name just like any other variable so company name in this case is a constant because we are using the final keyword and after the final keyword we are initializing it just like another variable right now let's see some benefits of using constants so first of all the value of the constant will not be changed by accident because as we said you will get a syntax error whenever you try to change the value of the constant also you don't have to type the same value if it is used multiple times so suppose that you are printing a string 10 times in your program so instead of typing best string 10 times you can store the value inside a constant and then you can use the constant 10 points so by doing this you will make sure that the value of the string will not be changed by accident and also if you want to change the string all you have to do is to change the value of the constant and now you might ask why don't I use a variable why should I use a constant of course you can use a variable the only difference is that the value of the constant can be changed all right finally a descriptive name for the constant will make the program easy to read and understand so now let's go to our IDE and see all of this in action so over here I'm still in the same project and I'm going to create a new one so let's go to file new project and over here we have the same window as before so I'm going to press next create project from template next and over here let's give a name for our project so for example nazo Academy alright and over here let's change the package name and of course this is optional alright so now I'm going to press finish it's asking me if I want to open this project in a new window or in the current window so I want to open it in this window alright and here is our new project ready now let's create a constant so I'm going to remove this comment and over here I'm going to create a constant string so first of all you will use the final key word and after that we will initialize a variable and in this case it will be a string so string let's say company underscore name is equal to the string nazar Academy alright like this and now let's print this constant so as out company name like this alright and now I'm going to press shift and Afton to run the program so as you can see this is our output now let's try to change the value of this constant so I will say company name is equal to for example another name right and as you can see we have an error this error says cannot assign a value to final variable company name so company name is a constant and we can't assign a new value to it all right now let me remove this assignment from over here so in this statement we are just declaring the constant and over here we are signing a value to it and this will not give us an error because this is the first time that you are assigning a value to our constant alright now after this statement let me try to assign a new value so let's say company name is equal to another name for example right so now we have an error the variable company name might already have been assigned to so because we already put a value inside our constant we can't do it again okay perfect now have a look at this code over here I'm printing this string and as you can see I type nice Oh wrong I put an eye and this should be an e right so to fix this I have to fix it over here and also in all these statements but let's suppose that I'm using this constant so I'm going to print it over here and I will remove all these statements now let me duplicate the statement like this and now in order to fix the mistake all I have to do is to fix it over here right so now it is fixed in all the places that I'm using my constant and of course we can use a variable to do this also but by using a constant we are sure that the value will not be changed by accident so this is it thanks for watching and I'll see you in the next video [Applause] [Music] you [Music]