Understanding of static variables and static methods for static modifiers

xiaoxiao2021-03-06  42

We know, as long as it is a class, no matter what class (including abstract class), there is only methods and variables inside, but to use this class, we generally walk two steps, such as an employee Employee

Employee S; / / Define a reference to EMPLOYEEEE

s = New Employee ("Aaron", 1980, 3, 1, 1500); // This really established an object S, that is, S point to a continuous area in memory.

- Note what is true, what is referenced -

If we want to change this area, you need to call the method, such as

S.raisSalary (25); // raiselary (double) method in EMPLOYEE

In this way, we change the variables in the class (class data or class variable) through the method (class method).

The so-called static method is to call through class name (of course, it is likely to call through class objects, just like the way Core Java, it is easy to confuse!)

And static variables are a shared variable (so anything is so easy to understand). Anything is called by class. It can change its value.

Regarding the static method (object-generated object) call static method, call the non-static method (by class name or object call), but the static method can call static variables without calling non-static variables (Error (12, 30): Non-static variable a cannot be Reference from a static context)

The following is a description of Static's description of Static in Thinking in Java:

2.6.3 Static Keywords typically, we will indicate the appearance and behavior of the object of the class when we create classes. Unless I created one object of that class with NEW, I actually didn't get anything. (Note, here is actually the reference and real object creation issues) Only the data storage is officially generated after the new NEW is implemented, and the corresponding method can be used. However, in two special circumstances, the above method is not available. One situation is just to use a storage area to save a specific data - no matter how many objects to create, even simply do not create objects. Another situation is that we need a special method that does not associate any objects of this class. That is to say, even if the object is not created, a method of calling can be called. To meet these two requirements, STATIC keywords can be used. Once there is something set to static, data or methods, it will not be contacted with any object instances of the class. So although an object that has never created that class, you can still call a static method or access some STITIC data. Before this, for non-Static data and methods, we must create an object and use that object to access data or methods. This is because non-Static data and methods must know the specific objects they operate. Of course, before formal use, because the Static method does not need to create any object, they cannot simply call other members, and do not reference a named object, thereby directly accessing non-Static members or methods (because non-Static members and methods You must associate together with a specific object). Some object-oriented languages ​​use two terms that "class data" and "class methods". They mean that data and methods exist as a whole class, not any particular object for that class. Sometimes you discover such a name in some other Java books. In order to set the data member or method to Static, simply need to define the preamp and this keyword. For example, the following code can generate a Static data member and initialize it: class statictest {static int i = 47;} Now, although we have made two StaticTest objects, they still only occupy a storage space of Statictest.i. . Both objects share the same i. Please examine the following code: StaticTest St1 = new statictest (); staticTest ST2 = New StaticTest (); this time, regardless of the same value 47, it is referenced by the same memory area. There are two ways to reference a static variable. As shown above, you can name it through an object, such as st2.i. It can also be used directly with its class name, and this is not working in non-statist members (it is best to use this method to reference the static variable because it emphasizes the "static" essence of that variable). Statictest.i ; where the operator will increase the variable. At this time, no matter the value of ST1.I or St2.i, it is 48. Similar logic is also suitable for static methods. You can use a special grammar format "class name. Method ()" by a special grammatical format. Method (). The definition of the static method is similar: class staticfun {static void inCr () {statictest.i ;}} It can be seen that staticfun's method INCR () increases the static data I.

Through the object, INCR (): staticfun sf = new staticfun (); sf.incr (); or, since INCR () is a static method, so it can be directly called by it: staticfun.incr (); Although it is "static", as long as it is applied to a data member, it will clearly change the creation of data (a class member, and a non-statist member of each object). If it is applied to a method, it is not so drama. For the method, Static an important use is to help us call the method without having to create an object. As will be seen later, this is crucial - especially when defining the program running portal method main (). Like any other method, the Static method can also create its own type of named object. So often use the Static method as a "leader", use it to generate a series of "instances". Here is my test program:

Package moonsoft.test.testdatatructure;

Public Class TestDriver

{

Public TestDriver ()

{

}

Public static void main (string [] args)

{

TestDriver TD = New TestDriver ();

String Month = Td.monthname (3); // Call Static Method through the object, not recommended

Month = TestDriver.monthname (4); // Standard call static Method

String month1 = td.monthname1 (3); // Call non-Static Method via object

Month1 = testdriver.monthname1 (4);

/ * Do not call non-Static Method:

* Error (14, 23): Non-static method monthname1 (int) cannot be reference from a static context * /

System.out.println (MONTH A); // Static method calls static variables

System.out.println (MONTH1 A); // Static method calls static variables

System.out.println (Month A1);

/ * Static method calls non-static variables

* Error (17, 30): Non-Static Variable A1 Cannot Be Reference from a static context * /

}

Public Static String Monthname (int MONTH) // Static Method

{

Switch (Month)

{

Case 1: Return "Jan";

Case 2: Return "Feb";

Case 3: Return "Mar";

Case 4: Return "APR";

Case 5: Return "May";

Case 6: Return "Jun";

Default: Return "Others";

}

}

Public String Monthname1 (int MONTH) // NOT A Static Method

{

Switch (Month)

{

Case 1: Return "Jan";

Case 2: Return "Feb";

Case 3: Return "Mar";

Case 4: Return "APR"; Case 5: Return "May";

Case 6: Return "Jun";

Default: Return "Others";

}

}

Public Static Int Add (Int T)

{

INT S = T;

S ;

Return S;

}

Public Static Double A;

Public Double A1;

}

In fact, it is very simple, and it is better to write a program test. It must be clearly known to what is going on in the mind.

转载请注明原文地址:https://www.9cbs.com/read-50962.html

New Post(0)