1, Member variable
1. Differences between member variables and local variables
- The position in the class is different: the member variable is outside the method in the class; A local variable is in a method or code block, or on a method declaration (in a parameter list)
- Memory locations are different: member variables are in the heap (static area in the method area); local variables are in the stack
- Different life cycles: member variables exist with the creation of objects and disappear with the disappearance of objects; Local variables exist with the method call or code block execution, and disappear with the method call or code block execution.
- Initial value: the member variable has an initial value
2. Use of member variables
grammar
[<Modifier >][static][final]<Variable type><Variable name>
Example (code below)
public class Hero { String name;//Hero name int age;//Hero age public static void main(String[] args){ //Create hero object hero1 Hero hero1 = new Hero(); hero1.name="Druid "; //Assign a value to the instance property name of hero1 hero1.age=45; //Create hero object hero2 Hero hero2 = new Hero(); System.out.println("hero hero1 Name of:"+hero1.name +"\t"+"Age:"+hero1.age); System.out.println("hero hero2 Name of:"+hero2.name +"\t"+"Age:"+hero2.age); } }
2, this keyword
1. this keyword represents itself
2. this keyword main purpose
-
Use this to represent the object of its own class (directly use this, use this to reference member variables, and use this - to call member methods).
-
Use this to call other constructors within its own constructors.
The test uses the this keyword in the method to reference member variables and member methods
The code is as follows (example):
public class Hero { public void jump(){ System.out.println("---The hero has encountered obstacles and needs to jump over---"); } public void run(){ System.out.println("---implement run Method hero is running---"); Hero hero = new Hero();//Create a Hero object System.out.println("her Object has been created memory address by:"+hero.hashCode()+",It will be implemented jump method"); hero.jump();//Call the run() method } } public class TestHero { public static void main(String[] args){ Hero hero = new Hero();//Create a Hero object System.out.println("hero Object has been created memory address by:"+hero.hashCode()+",It will be implemented run method"); hero.run();//Call the run() method } }
The instance variable has the same name as the local variable (there is no this before the instance variable)
The code is as follows (example):
public class Hero { String name; public void setName(String name){ name=name; //It is easy to be confused here and has poor readability } public static void main(String[] args){ Hero hero=new Hero(); hero.setName("Storm Elf"); System.out.println("hero Your name is:"+hero.name); } }
The instance variable has the same name as the local variable (this precedes the instance variable)
The code is as follows (example):
public class Hero { String name; public void setName(String name){ this.name=name; } public static void main(String[] args){ Hero hero=new Hero(); hero.setName("Storm Elf"); System.out.println("hero Your name is:"+hero.name); } }
3, Hide and encapsulate
Encapsulation is one of the three characteristics of object-oriented.
1. Purpose of good packaging:
- A predetermined method accesses data, and control logic is added to the method to limit unreasonable access to attributes.
- Data checking is helpful to ensure the integrity of object information.
- Easy to modify and improve code maintainability.
The essence of Java encapsulation: use the access control character private to hide attributes and public to expose methods.
public class Hero { //Use private to decorate attributes and hide them private String name; private int age; //The public method exposes an assignment to the name attribute public void setName(String name) { //Perform reasonable verification. The user name must be between 2 and 6 if(name.length()>6||name.length()<2){ System.out.println("The name you entered does not meet the requirements"); return; }else{ this.name = name; } } //The public method exposes the value of the name attribute public String getName() { return name; } ...... }
2. Access control modifier
Example: the property modified by the private access control character can be accessed in the same class
public class Person{ private String name="Robin Li"; public static void main(String args[]){ Person person = new Person(); //All properties can be accessed in the same class System.out.println("Name is:"+person.name); } }
Example: the attribute modified by the default access control character can be accessed in the same package
package com.vo; public class Person{ //The default modifier is default String name="Robin Li"; } public class Test{ public static void main(String args[]){ Person person = new Person(); //Access default decorated properties System.out.println(person.name); } }
Example: attributes modified by public access control characters can be accessed by classes in different packages
package com.vo; public class Person{ //Public is a public access modifier public String name="Robin Li"; } package com.test; public class Test{ public static void main(String args[]){ Person person = new Person(); //Properties modified by public can be accessed anywhere System.out.println(person.name); } }
4, static keyword
- The running of Java classes includes two stages: class loading and instantiation.
- When a class is loaded into the JVM, static members are initialized.
- Static members do not belong to an object, but only belong to the class of static members.
Members modified by static are static members.
Static code block
Static properties
Static method
1. Static member variables
Static member variables can be accessed directly through classes or through instances of classes.
Example: 10 withdrawals from simulated personal bank accounts
public class Bank { //Static member variable, account balance private static int count = 50000 ; public static void main(String[] args) { //Instantiate 10 Bank objects for(int i=0; i<10; i++){ Bank bank=new Bank(); //Every time you withdraw 1000 yuan, the instance of the class calls the static member property bank.count = bank.count - 1000; //Bank.count=Bank.count-1000; // Access static members between class names System.out.println("Current total bank money="+Bank.count); } }. }
2. Static method
- Methods modified by static are called static methods.
- Static methods can also be accessed directly through class names or object names.
- Static methods cannot access non static members, such as instance properties and instance methods.
- Static methods are usually used as tool methods, because static methods will not affect the final execution effect of methods due to different instances.
Example: define a static square to calculate the square
public class MathUtils { //Calculate square public static double square(double num){ return num*num; } public static void main(String[] args){ double num=9.6; double result=MathUtils.square(num); System.out.println(num+"Square of="+result); } }
3. Static code block
When do we need to use static code blocks:
If you need to perform an operation when the class is loaded, you can use a static code block
Examples;
public class StaticBlock { //First static code block static{ System.out.println("---First static code block---"); } //Second static code block static{ System.out.println("---Second static code block---"); } //Third static code block static{ System.out.println("---Third static code block---"); } public static void main(String[] args){ System.out.println("---main Method was executed---"); } }
summary
- Variables can be divided into member variables and local variables. Member variables are variables defined in class scope, and local variables are variables defined in methods.
- The this keyword represents itself.
- The visibility range of Java access control characters from small to large is: Private - > Default - > protected - > public.