JAVA learning notes (9)-initialization block and static code block, java learning notes
Initialization Block
/** Initialization block * initialization variable method: block of code declared, constructor, and code block * modified with the static keyword, called the static code block ** execution sequence: static code block> constructor ** the static code block is executed only once during class loading, it is mainly used to initialize static variables * code blocks and constructor. Execute */public class Test01 {public static void main (String [] args) when creating an object) {Car car = new Car (60000); // create an object car. show (); // call the object method System. out. println ("------- gorgeous separation line ------- \ n"); Car car2 = new Car (80000); car2.show ();}} /** automobile class */class Car {// attribute String brand = "Chery"; // brand, instance variable double price; // price String color; // color static int seat; // number of seats, static variable public Car () {System. out. println ("constructor without Parameters");} // constructor with parameters, used to initialize the public Car (double price) {System. out. println ("initialize the variable price through the constructor"); this. price = price;} // code block {System. out. println ("initialize variable color through code block"); color = "red";} // static code block static {System. out. println ("initialize variable seat through static code block"); seat = 5;} public void show () {System. out. println ("brand:" + brand); System. out. println ("price:" + price); System. out. println ("color:" + color); System. out. println ("Number of seats:" + seat );}}
Static variables and code blocks
/** Static * static variables (class variables) * static variables are shared by all objects of the class, that is, all ** static member access methods of the class: name pair. static variable or class name. static variable ** conclusion: * 1. static can modify variables, methods, code blocks, and anonymous Classes * 2. static member initialization during class loading * 3. static methods can only access static members, but cannot access non-static members * 4. in the common member method, you can access the static member's *** variable type **. the variables are divided by scope: local variables and member variables: instance variable, static variable */public class Test02 {public static void main (String [] args) {Student stu1 = new Student ("Zhao Xin"); stu1.age = 20; stu1.holobby = "Learning Java programming"; System. out. println ("Zhao Xin's hobby:" + s Tu1.hober); // access the static variable stu1.show () through the object name; stu1.holobby = "Warcraft"; Student stu2 = new Student ("galun", 35 ); // stu2.holobby = "Learning Java programming"; System. out. println ("galun's hobby:" + Student. holobby); // access the static variable stu2.show () through the class name; Student stu3 = new Student ("cold ice", 18, "Learning Java programming"); stu3.show (); stu3.print (); // use the object access static method Student. print (); // use the class name to access the static method}/** Student class */class Student {String name; // instance variable int age; // instance variable static String holobby; // Static variable // construction method without parameters public Student () {}// construction method with one parameter public Student (String name) {this. name = name;} // constructor with two parameters: public Student (String name, int age) {this. name = name; this. age = age;} // constructor with three parameters: public Student (String name, int age, String holobby) {super (); this. name = name; this. age = age; this. holobby = holobby;} // outputs the student information. The common member method is public void show () {System. out. println ("name:" + name +", Age: "+ age +", hobbies: "+ holobby +" \ n ");/** you can access static members in common member Methods */print (); system. out. println (holobby);} // static method public static void print () {System. out. println ("I Am a static method ..... "); // System. out. println ("output in static method-name:" + name); // non-static member System cannot be accessed in static method. out. println ("output in static method-hobby:" + holobby); // only static members can be accessed in static method }}
Class Member
/** Class member ** this indicates the current object * 1. member in category * 2. resolve conflicts between local variables and member variables * 3. call the overloaded Construction Method */public class Test03 {public static void main (String [] args) {Dog dog = new Dog ("wangcai", 2, "female"); dog. show () ;}/ ** Dog class */class Dog {// static variable static String holobby; // member variable String name; int age; String sex; // customize the construction method public Dog () {super () without parameters; // call the construction method System without parameters of the parent class. out. println ("construction method without Parameters");} public Dog (String name) {this (); // call class System. out. println ("constructor with a parameter"); this. name = name;} // The overloaded constructor. The constructor with two parameters is public Dog (String name, int age) {this (name ); // call the overloaded constructor in the class this. age = age; System. out. println ("constructor with two parameters");} // reload constructor, a constructor with parameters, used to initialize the public Dog (String name, int age, string sex) {this (name, age); // call the overloaded constructor this in the class. sex = sex; System. out. println ("constructor with three parameters");} // member method public void show () {System. out. Println ("name:" + name + ", dog age:" + age + "Gender:" + sex);} // static public static void play () {System. out. println ("you are taking the Frisbee .... ");} // Common code block {System. out. println ("common code block"); sex = "";} // static code block (static initialization block) static {System. out. println ("static code block"); holobby = "";}}