Package Penalty for cn.gl.ObjectLearn; / ** * Static modifiers: * modified by the static variables, constants, and methods are called static variables, constants and static methods static, they are stored in a "static area" of memory, a * these variables and methods an independent life cycle. * Static memory area will be released after the end of the entire program is running, so with the life cycle of a static modification of code is the entire life cycle of the program. * Static District: * Memory variable in a static area can be shared in this class, when this class static variables and static method calls other classes, no need to instantiate can call. Static variables *: * different instances of the same object class share the same static variable, if the object to change a static variable of another object also changes. * Static constants: * Use a modified final static member variables, this will become a static member variables constant. * Static methods: * want to use the class member method, you need to first class instantiation, but do not want or can not create an object, we can use the static method. * Call the static method without creating an object class. * Note: * In fact, we have been using System.out.println () method is a typical static method, * we do not create the System object, to achieve the output. Main method in the class is also a static method. * Static block of code: * in addition to members of the class methods, static is a modified static block code region. * Define a static block, the initialization operation can be completed, it will run when the class declaration. * Pool.water);@author cold rainy night Sleepless-mail E: [email protected] * @version Created: November 9, 2019 * / public class ObDemo6 { public static void main (String [] args) { Pool OUT = new new pool (); pool in = new new pool (); System.out.println ( "pool of water" + Pool.water); System.out.println ( "water twice a pool" ); in.inlet ( ); in.inlet (); System.out.println ( "pool of water" + out.outlet (); System.out.println ( "pool of water" +Pool.water); StaticVariable A = new new StaticVariable (1,2 ); StaticVariable B = new new StaticVariable (13,17 ); System.out.println ( "the value ax =" + ax); System.out.println ( "ay is a value =" + ay); // StaticVariable StaticVariable new new B = (13,17); System.out.println ( "BX is a value =" + BX); System.out.println ( "by the value = "+ by); Circular C = new new Circular (3.0 ); Spherical S = new new Spherical (3.0 ); // Graphical.PI = 1.1; static error constants can not be modified StaticMethod.show (); // not create object class, direct call StaticTest the Test = new new StaticTest (); test.method (); // if the comment sentence, the sentence will only output 3 } } class Pool { static public int Water = 0 ; public void Outlet () { IF (Water> = 2 ) { Water = Water - 2 ; } the else { Water = 0 ; } } public void Inlet () { Water = Water +. 3 ; } } class StaticVariable { static int X; // static variable int Y; public StaticVariable ( int X, int Y) { the this .y = Y; the this .x = X; } } class the graphical { // graphical class Final static Double the PI = 3.1415926; // static constant as far as possible be capitalized } class Circular { // round type Double RADIUS; Double Area; public Circular ( Double RADIUS) { the this .radius = RADIUS; Area = Graphical.PI * * RADIUS RADIUS; the System. Out.println ( "circle radius =" + rADIUS); System.out.println ( "area of a circle =" + area); } } class Spherical { // ball Double rADIUS; Double Volume; publicSpherical ( Double RADIUS) { the this .radius = );radius; Volume =. 4 / Graphical.PI. 3 * * * RADIUS * RADIUS RADIUS; System.out.println ( "radius of the sphere =" + RADIUS); System.out.println ( "sphere volume =" + Volume); } } class StaticMethod, and { static public void Show () { System.out.println ( "static method I" ); } } class StaticTest { // test code execution order public StaticTest () { System.out.println ( "here constructor " } { System.out.println ( " this is a non static code block " ); } public void Method () { System.out.println ( "herein is a member of method" ); } static { System.out.println ( "here static code block " ); } }
Output:
0 cistern water
to the pool water twice
cistern water. 6
. 4 cistern water
value is a.x 13 is =
A. Y. values are 2 =
b.x value is 13 is =
b.y = 17 values are
circle radius = 3.0
= 28.274333400000003 area of the circle
radius of the sphere = 3.0
volume of sphere = 84.82300020000001
I static method
where static code block
where non-static block
here is a configuration method of
this method is a member