1. Instance variables and class variables

Instance variables: each object is independent. Modifying an instance variable of an object does not affect the values of other instance variables. The variable values are not modified by the static keyword.

Class variable: a class variable shared by all objects. If the value of the variable is changed for one object, the value of the variable is changed for all objects. Use the static keyword.

2. Static and non-static methods

Static method: a method that uses the static keyword. It can be accessed using the class name. Method or object. Static methods can only call static methods and use static members, because static members and methods will be loaded with the class, the loading of non-static members or methods is later than that of static members and methods, so static methods can not call non-static methods and non-static members, that is, static to static;

Non-static method: refers to a method that does not use the static keyword modification, and is generally accessed using object. Method. Non-static methods can be called and used with non-static methods or static members.

3. The role of packages in Java

In Java, the package declaration is usually written in the first line of the file, using the keyword package + package name. (1) Avoid duplicate names, for example, sometimes when we call a method of a class, we will find that there are methods with the same class name, at this time you need to import the package of the class you want to call or use the full class name to call, so as to avoid duplicate names; (2) For permission control, classes in the same package can be called each other, if you call classes in other packages, you must import the package where the calling class is, so as to avoid external illegal calls. In addition, the key words public, protected and private of access control permission can be reasonably used to set the access permission (see Attached Table 1 for more control of each key word). (3) Divide the project structure level to make the file management more organized, such as MVC and MVP subcontracting;

Static import in Java

Static imports is JDK1.5 introduce new features, in general, we call the static members of a class or a static method use “the name of the class. Properties” to call, and static imports can be put under a class static member (method and variable) using similar imported guide of packages, which can be directly imported into the level of the members of the class (method and variable), This allows the static variables and methods of the imported class to be directly visible in the current class. Using these static members does not need to use the “classname.attribute” method to call the method name or attribute, as easy as calling the methods and attributes of your own class.

The syntax for static imports is:

  • Import static Package name. Static member variables;
  • Import static Package name. Static member functions;

Note that static member variables and method names are imported. You can also import all static members of a class:

  • Import static Package name. The name of the class. *;

* stands for wildcard ownership.

Normally we can moderate the use of static imports, a lot of abuse is not recommended, because we know that Java is an object-oriented language, using static imports invoked, missing the name of the class, which weakens the class description, sometimes we don’t know which members of the class, call may be thinking, weaken the object-oriented thought, Causes the class and between the class call relationship is not clear!

5. This keyword in Java

In Java, when an object is created, the Java virtual machine assigns it a pointer to the object itself, which is** this**. When the layout variable of a method has the same name as the instance variable, use this keyword to distinguish it. If the layout variable has the same name as the instance variable, use this keyword to distinguish it. ② Use this. Method name (parameter); Call a member method of a class, usually without this; ③ Use this(parameter 1…) ; To call other constructors in this class, such as a custom View in Android, usually write three overloaded constructors, single-argument constructor uses this(parameter 1, parameter 2); To call the two-parameter constructor, which calls the three-parameter constructor; (4) Pass this as a parameter. In Android activities, this is often passed as a parameter. ⑤ If this is used in the inner class, the name of the outer class must be used in order to call the method of the outer class. The method name (); To invoke the;

There are a few other things to note when using this:

  • Using this in a constructor to call another constructor must place the call in the first line of the method and only be used once;
  • We can no longer use this to call constructs in instance methods, because instance methods must have objects, and object creation must call constructors, which should be called before instance methods.
  • We cannot use this to call each other’s constructs in constructs. For example, if we call two-argument constructs in one-argument constructs, and one-argument constructs in two-argument constructs, we use the one-argument constructs to create an instance, calling as follows: Single-argument constructs -> double-argument constructs -> single-argument constructs, which can be called indefinitely. Additionally, this violates the rule that you can only call a constructor with the same argument once in a method’s call chain.

6. Super keyword in Java

  • super(); The parent class of the call is Object’s no-parameter construction, which is automatically called by default and generally omitted.
  • This and super keywords are to solve the problem of duplication, if there is no duplication problem can be omitted;
  • Both this and super can be understood as a reference to the parent class, that is, properties and methods in the parent class can be called;
  • Super. method name (); Represents the method that calls the parent class;
  • The super. property calls the (rarely used) property of the parent class.
  • **super(); ** Calls the parent constructor;
  • Both this and super can only be placed on the first line of a method, so they cannot appear together;
  • Super can only appear in constructors;

In Java, all classes have an Object parent. If a class does not extend its parent using the extends keyword, it inherits the Object class by default. Subclasses do not inherit the constructor of their parent class, but they do call the constructor. Otherwise, errors will be compiled;

7. Overloading and rewriting in Java

Overload. In the same class, several methods with the same method name and different parameter lists are called Overload methods.

Override, in the child parent class, the subclass and the parent class method name must be the same, parameter list must be the same; For the return value type, the subclass is either the same as the parent class, or a subclass of the parent class return value type; For permission modifiers, the subclass’s permission modifiers are either the same or broader than the parent class’s permission modifiers. If an exception is thrown, the scope of the exception caught by the subclass is either the same or smaller than that of the parent class, that is, the exception of the subclass is a subclass of the parent class.

8. = = and equals

The relational operator “==” produces a Boolean result. It evaluates the relationship between the values of the operands, comparing their values in the case of primitive data types, or the memory address of the objects in the case of reference data types.

Equals method is a method in a base class Object, all the classes are directly or indirectly inherited in the Object class, so all class has the method, without rewriting the equals () method, calling the equals () method and the effect of using = =, are also compared in memory address values; In most classes provided by Java, such as String, Double, Date, Integer, etc., the equals method is overridden to compare whether the contents of the objects to which it refers are equal. If it is our own class, if we do not override the equals method, The equals method is used to compare objects’ in-memory addresses by default. To compare content, you must override equals.

9. Understanding classes

Class is an abstract description of a class of things, objects are instances of the class, can be understood as a kind of auto design, object is the real production out of the car, a design (class) can be produced more cars (object), the class contains attributes and methods, and attributes describe the characteristics of the class are, method describes the specific function of a class, So that we will pay more attention to when design class a class, the function and characteristics of their abstract partial shipments to the class, when external call don’t need to care about the implementation of class, so you can more consider the relationship between classes and between classes, the benefits of this is can improve the reusability of objects, reduce the development difficulty, Most of the time Java is object-oriented programming, that is, Everything is object. For object-oriented development, generally divided into three steps: OOA (object-oriented analysis), OOD (object-oriented design), OOP (object-oriented programming). OOA refers to analyzing features or methods based on specific functions. OOD refers to designing concrete classes based on features and methods. OOP establishes associations between classes and classes that call each other.

Variables, memory, and garbage

A variable is a storage area in memory whose value is variable, not fixed;

Memory in the Java generally divided into Java virtual machine (stack and heap memory, the stack is usually stored in basic data types and reference types reference variables, heap memory is stored in an object instance, the Java heap is a Java virtual machine memory management by the largest, it is Shared by all threads of a memory area, created in the virtual machine startup, It can not be physically continuous, as long as it is logically continuous; In addition, there are the concepts of method area, static constant pool, program counter, local method stack and other memory allocation areas.

Garbage: Refers to an object that has no reference to it (a piece of heap memory space). This space is garbage, and all garbage is waiting for the GARBAGE collector to collect (free memory space) from time to time.

More articles on dry goods follow my wechat official number: