The static modifier can be used for:
- variable
for example type: int
2. methods
for example void method
3. block
for example class with default modifier
The static modifier will link the variable, method, block or nested class to its class. This means that you could call the variable/method/block/nested class (which is made static) from another class by only calling the class its name. So you don’t have to make a new object.
So, if you have 2 classes:
//normally do you create classes in different tabs, but you could also do them in one, this is just for the example.
This is an example when you use the static modifier for variables. But it will work the same for the others.
So normally you create an object:
and then do you write:
So you program looks like this:
When you use the word static you don’t have to create a new object. You just have to implement in the void start():
Then you whole code looks like this:
output:
5