Instantiation of an object
1.1 Instantiation steps:
1 Load Class into Static Zone
2 new creates memory space in heap memory
3 Call the construction method to initialize the space and copy member properties into it
4 The construction method finishes executing the stack and assigns the address of the heap memory object to the variable o
Each object has a separate space, and the member variables stored in each object do not affect each other:
public class OOP_01 { int age; public static void main(String[] args) { OOP_01 o = new OOP_01(); // Every new time a new object is created OOP_01 o1 = new OOP_01(); // Change the value of the age property in the o1 object o1.age = 2; // Call the member variable age in o, which is 0 System.out.println(o.age); // Call the member variable age in o1, which is 2 System.out.println(o1.age); System.out.println(o); } }
1.2 javaBean
JavaBean: JavaBean is a public Java class, but there are at least three conditions to satisfy for editing tool recognition: there is a public default constructor (such as a parameterless constructor,)Attributes are accessed using the public get, set method, that is, set to private, and get, which corresponds to the size of the property name. For example, property name, get method is written, public String getName(){},N is capitalized. Serialization is required. This is a framework where tools must be viewed recently to reflect state across platforms <Think in Java>,It says that JavaBean was originally implemented for visual programming of Java GUI. When you drag an IDE build tool to create a GUI component (such as a multi-checkbox), the tool actually creates a Java class for you, exposes its properties, modifies it, and exposes the event listener.
EJB: In enterprise development, scalable performance and transaction, security mechanisms are required to ensure smooth development of enterprise systems, rather than developing to a scale and replacing a set of software systems. Enterprise Bean appears when there are increased protocol requirements. EJB puts forward some requirements on the basis of javabean, which is of course more complex.
POJO: A person named Josh MacKenzie thought that EJB was too complex to be used every time, so he invented POJO. POJO is a normal javabean. What is normal is corresponding to EJB. In short, the difference is that you first decide if the condition of JavaBean is met, then if you implement some requirements, it is EJB, otherwise it is POJO.
1.3 Common exceptions in object calls
When an object's value is assigned to null, a null pointer exception occurs when the member method or member property inside the call is =
If we invoke static attributes within a class, there will be no null pointer exception, because the object is stored in heap memory, static attributes, root and heap memory have nothing to do with it. When we invoke static attributes using a reference type variable, it will be converted to the corresponding class name at compile time, so whether null or not,They all convert to class name calls, so static variables are properties and values common to all objects
1.4 Passing Values and References
Class is a reference type and address is passed
public class OOP_04 { public static void main(String[] args) { int i = 10; m1(i); System.out.println(i); System.out.println("===="); A a = new A(10); m2(a); System.out.println(a.age); } public static void m2(A a) { a.age++; System.out.println(a.age); } public static void m1(int i) { i++; System.out.println(i); } } class A { int age; public A(int age_) { age = age_; } }
1.5 Variable Priority
1. Static and member variables cannot have the same name
2. Local variables can have the same name as static and member variables
3. When a local variable has the same name as a static variable, the local variable takes precedence over the static variable in the method
4. Class name distinction is required to use static variables
public class OOP_05 { // Static and member variables cannot have the same name int a = 1; static int a1 = 2; public static void main(String[] args) { // Variables that cannot have different names in a scope int i = 2; // int i=1; // Local variables can have the same name as static and member variables int a = 4; int b = 5; // When a local variable has the same name as a static variable, the local variable takes precedence over the static variable in the method. System.out.println(b); // To use static variables, you need to distinguish them by class name System.out.println(OOP_05.a1); } }
Two This
What is 2.1this?
this: is a member variable of the reference type that holds its own memory address in each object
this represents the object itself.
2.2 What can this do?
1 Used in member and construction methods, you can distinguish between local variables with the same name and member variable syntax: this.xxx = xxx;(
public class This_01 { public static void main(String[] args) { // TODO Auto-generated method stub This_01 t = new This_01(1); t.setAge(10); System.out.println(t.age); } int age; public This_01(int age) { this.age = age; } public void setAge(int age) { this.age = age; } }
2 Used in a construction method to overload calls to other construction methods in the current class to improve code reuse syntax: this (parameter);Must be written on the first line of the construction method
public class This_02 { public static void main(String[] args) { MyDate date1 = new MyDate(); // October 0, 0, 0 date1.print(); date1.setYear(2021); date1.setMonth(10); date1.setDay(12); // October 12, 2021 date1.print(); // Requirement: When creating a time object, there must be a year, month, day (full-parameter construct) MyDate date2 = new MyDate(2008, 8, 8); // 8 August 2008 date2.print(); // Requirement: When creating a time object, you can pass in the year, month, day or not // If not passed, the default is January 1, 1970 (one full parameter, one none) MyDate date3 = new MyDate(); // January 1, 1970 date3.print(); } } class MyDate { // Member variables private int year; private int month; private int day; // Total parameter construction public MyDate(int year, int month, int day) { this.year = year; this.month = month; this.day = day; // xxxx } // Parametric construction public MyDate() { // this.year = 1970; // this.month = 1; // this.day = 1; // xxxx // Overload calls the parametric construct above for code reuse // But the statement must be on the first line of the construction method this(1999, 8, 20); } // getter/setter public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } // Requirements: Print year, month and day in specified format public void print() { System.out.println(year + "year" + month + "month" + day + "day"); } }
3 return this makes chain calls
Core Principle: The return value of the former must call the reference of the latter
In a member method, which object calls this method and which object is this?
public class This_03 { public static void main(String[] args) { This_03 t=new This_03(); t.m1(); t.m2(); System.out.println("======"); // call chaining // The return value of the former must be a reference that calls the latter t.m1().m2(); } public This_03 m1() { System.out.println("I am m1 Method"); return this; } public This_03 m2() { System.out.println("I am m2 Method"); return this; } }
3 static
3.1 Static statement block
Static is a modifier to distinguish static from member
Static modifies three things: static variables, static methods, and static statement blocks
Static Variables: Variables in the class body modified with static that are initialized when the class is loaded
Static methods: methods decorated with static in class bodies
Static statement block: A code block (statement block) decorated with static, a {} is a statement block
Syntax: static {java code}
(Executed during the class loading phase, immediately after loading is completed, before main, and only once. Since it is persisted first and executed only once, it is appropriate to do some preparatory initialization operations.)
public class Static_01 { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("main Method"); } // Multiple statement blocks, executing from top to bottom static { System.out.println("Static statement block 1"); } static { System.out.println("Static statement block 2"); } }
3.2 Instance Statement Block
Instance Statement Block: Can be seen as a member method without a name, that is, a statement block without a static modification.
Each time an object is created, it is executed once, many from top to bottom.
Syntax: {code;}
public class Static_02 { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("main Method"); Static_02 s = new Static_02(); Static_02 s1 = new Static_02(); } public Static_02(){ System.out.println("Construction method"); } static{ System.out.println("Static statement block"); } { System.out.println("Instance statement block"); } }
Static variables are also initialized during the class loading phase, and are initialized from top to bottom, consistent with the static statement block priority