One of java static learning

        Static is a very magical keyword. When I left school to do engineering, it was one of the more used keywords, and it was also a more important keyword.

        When to use static? Usually in a common method, such as the shared operation of connecting to the database, and the public parameter of the database connection parameter, if it is defined as static, there is no need to allocate a copy for each object.

        Static cannot be used indiscriminately, because jvm generally does not actively recycle statically modified variables. It coexists with classes. The life cycle starts from the moment the class is loaded and ends when the current project ends. If a project's There are too many static variables and methods, which consume memory and are prone to out of memory errors. . . (The outof errors I've had so far are all because there are too many statics...because it's so convenient)

        In the following code, eclipse prompts an error, Cannot make a static reference to the non-static field b, and non-static variables cannot be called in static methods. This is because the static method follows the class, it will be loaded when the class is loaded, and the instance variable (that is, the non-static variable) follows the object, so when the static method is called, it is very likely None of the instance variables are loaded, so java enforces you not to call instance variables directly to avoid this error. In instance methods, static variables can be called.

static int a = 1;
	int b = 1;
	public int a(){
		return a;
	}
	public static int sa(){
		b = 1;
	}

        If you have to call instance variables, you can do this:

static int a = 1;
	int b = 1;
	public int a(){
		return a;
	}
	public static int sa(){
		new staticTest().b = 1;
		return 0;
	}

           Use objects in non-static methods, calling instance variables.

           A personal metaphor for static parameters, only for those who don't quite understand it, the gods go around:

            Just like the car class, it has many variables and methods, any company can use its variables and methods to build its own car (object), just like QQ and Mercedes-Benz, they are all cars and have car attributes (parameters and method), they can build a cool car (a specific object of the car class) according to their own ideas and preferences. Now suppose that the car class has a static variable (such as the regulations of the Automobile Association), which stipulates that only those with wheels can call a car. Then, anyone and any object can call this static variable of the car class (of course, the premise is that he allows calling (is it public)), check if an object is a car. People can also call the static parameters of the car class through a specific car (concrete object) of Chery. At this time, if Chery modifies the static parameters of the car class, such as changing the car to be called without wheels, then it may affect all cars (all objects) of Mercedes-Benz in an instant. All objects belonging to the car call this parameter, and they will find that It turned out to be called a car without wheels. . . Therefore, once the static parameter is set, do not modify it easily. Once modified, it will affect all instantiated objects of this class (like all the cars of the car company in this example).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326242683&siteId=291194637