First, member variables and local variables (think)
A different position in a class: a member variable in a class, outside a method, a local variable within a method, or a method declaration
Different locations in memory: Member variable heap memory, local variable stack memory;
Different life cycles: member variables exist as objects exist, local variable variables exist with method calls, and end of method calls disappear
Initialization values are different: member variables have default initialization values, local variables are not, and assignments are defined before they can be used.
The value passed when the parameter is passed
Ii. Initialization of classes
Load class into memory
= = "In the stack memory for S Open space
= = "opens up space for student objects in heap memory (new Class)
= = "Default initialization of the member variables of the student object;
= = "Default, display initialization for the members of the student object;
= = "Assigns the member variable of the object by constructing the method;
= = "The Student object is initialized and the Douchienne address is assigned to the S variable.
Third, the static keyword
Modifying member variables and Member methods
Characteristics
Load as the class loads
Precedence over object presence (static zone loaded in Class)
shared by all objects of the class (determines whether static keywords are used)
Can be called by the class name (Student.say ();)
Role
The This keyword is not in a static method
Static methods can only access static member variables and static member methods
1. Static methods cannot access non-static member variables
2. Non-static methods can access static member variables
Iv. the difference between a static variable and a member variable
belong to different
Static variables belong to classes, so they are also referred to as class variables
Member variables belong to an object, so also known as instance variables (object variables)
Different locations in memory
Static variables stored in the static area of the method area
member variables stored in heap memory
Memory occurrence time is different
Static variables are loaded as the class is loaded and disappear as the class disappears
Member variables exist as the object is created and disappear as the object disappears
Call different
A static variable can be called by a class name, or it can be called by an object
Member variables can only be called by object name
Static code block constructs code block local code block
Local code block
To appear in a method; limit a variable's life cycle, release early, and improve memory utilization
Construct code block format: {EXECUTE statement}
Occurs outside of a method in a class, the same code is stored together in multiple constructor methods, each call construct executes, and is executed before the method is constructed
Static block of code (appears outside the method in the class with the static modifier)
Occurs outside a method in a class, with a static modifier, to initialize the class, execute at load time, and execute once.
Static code blocks take precedence over building blocks of code, and creating objects executes building blocks of code.
1 /*Dynamic member Methods2 non-static member methods access static member variables3 */4 classDemo_35 {6 Public Static voidMain (string[] args)7 {8Students s =NewStudents ();9s.b = 13;Ten S.say (); One } A } - classStudents - { the intA; - Static intb; - //non-static member methods access static member variables; - Public voidsay () { + System.out.println (b); - } +}
non-static member methods access static member variables
/*static Member method static member method access non-static member variable result: Cannot access apply non-static variable from static context*/classdemo_5{ Public Static voidMain (string[] args) {Students s=NewStudents (); S.A.= 13; S.say (); }}classstudents{intA; Static intb; //static member methods access non-static member variables; Public Static voidsay () {System.out.println (a); }}
static member methods access non-static member variables
V. The concept of succession
1. Inheritance
Features: Only single inheritance is supported, multiple inheritance is not supported (only one parent class)
Support Multilayer inheritance, class a{} Class B extends a{} Class C extends b{}
Role: Improve code reuse
Improve the maintainability of your code
Having a relationship between classes and classes is a precondition for polymorphism (inherited drawbacks, particularly strong coupling)
2.super usage and this
Definition: This represents the corresponding reference for this class,
Super represents the identity of the parent storage space (parent class reference)
Usage: Access member variable this. member variable super. Member variable
Access construction Method This (...) super (...)
Access Member method this. Member method () Super Member method ()
Note: When you inherit data from a parent class in a subclass, the parent class's data is used, and the subclass initializes the parent data before initializing it
The first statement of each construction method is super () by default
Java Notes 6