java in the static keyword (rpm)

static - static variables

We all know that we can create multiple objects based on a class of this class, each object has its own members, independent of each other. However, at some point, we hope that all members share the same class object. Static at this time is the time to show their talents! !

Java was modified static members are called static members or class members. It belongs to the class of all, not all of an object, ie the class of all objects are shared. Static members can directly access the class name, object name can be used to access it. Of course, given the special nature of his role is more recommended to use the class name to access ~~

You may be modified using the static variables, methods and code blocks.

For example, we define a static class variable Hobby, operation code as follows:

operation result:

Oh, to pay attention to: Static members belong to the entire class, when the system for the first time this class, it will assign the memory space will be uninstalled until the class is recycling! ~~

 

static - static method

As with static variables, we can also use static modification method, called static method or class method. In fact, we have been written before the main method is a static method. Use static methods such as:

operation result:

requires attention:

1, a static method can call the same static members, but can not call non-static member directly. Such as:

If you want to call non-static variable in a static method, you can create a class of objects, then to access the non-static variables through an object. Such as:

2, in the conventional method, a member, can directly access the non-static and static variables are the same, as follows:

3, static methods can not directly call a non-static method, you need to access non-static methods through an object. Such as:

Guess you like

Origin www.cnblogs.com/banml/p/11353082.html