I just can not understand why they are. In the books they write that initialization is their first. Remove magic numbers? So it can be done with a regular variable. Yes, it is convenient to contact (SomeClass.var). Global scope? So a normal variable can change the access modifier to public.
1 answer
Put you have a class dbhelper. It has a constructor that takes the name of the database to which it should connect. We assume that you have only two such databases - you create public static variables in the DbHelper 2 class and select one of them when you create an object of your DbHelper to connect to a specific database. It will look like this in the end:
DbHelper helper = new DbHelper(DbHelper.TABLE_CATS); If static variables did not exist, then you would not be able to create an instance of this class. You would have to create extra variables in each class where you connect to the database.
There are many other examples of using static variables, but it’s incorrect to talk about their advantages over ordinary variables, since This is a separate tool for individual tasks.
staticwill allow you to store a variable without having a single class object. As an example, if you need to limit the number of class objects created. - Sanek Zhitnik Sep.