11.6 The stactic statement

Cliquez içi pour plus de vidéos disponibles sur notre chaine youtube !

Hello everyone in this video, let’s make a little reminder about the keyword "static" So, in the java programming language the keyword static indicates that the member in question belongs to himself and not to an instance of this type. This means that only one instance of this static member is created and shared by all instances of the class. We will make an example by creating a class called Static Test. And as an int variable a That is equal to 1997. A string name that is equal to Tom. So, to access these variables you must create the StaticTest object. First, we will create an int int b interger variable equal to 2022 Then, we will create the static test object. And as variable name "s" = new Static test Then, let’s display our variables it will be "sout". My name is More s.name plus My age is more In parentheses b - s.a. And if we launch our program, And as we can see, it works very well So, we can access our variables like that but very it’s annoying and nobody wants to do that every time. So, by putting a static variable. once the variable is declared static the memory is allocated only once and not every time a class is instantiated. Therefore, you can access the static variable without reference to an object. So it’s simpler. So I go back to my hand class. And I delete the Static test object Now I replace it with the name of the class, so it will be Static Test. a And if we relaunch the program, we can see that we have the same result. That’s all for this video, it was a little reminder about the static Go! I’ll tell you next time.