Java se Chapter 4 - object oriented (medium)
Objects and references
In the Java language, variable types other than basic types are called reference types.
Java objects operate on them by reference.
For example, we have a custom class car (automobile class)
class Car{
String color;
String name;
String model;
}
Car car = new Car();
The action of this statement is usually called creating an object. In fact, it contains three actions.
1) The new Car () on the right takes the Car class as the template, calls the constructor of the Car class, and creates a Car class object in the heap space.
2) Car on the left creates a car type reference variable. The so-called car class reference refers to the object reference that can be used to point to the car object in the future.
3) The "=" operator makes the object reference point to the Car object just created.
We can split this statement into two parts:
Car car;
car = new Car();
In this way, it is clear that there are two entities: one is object reference; Variables, one is the object itself.
new Car(); this statement creates entities in heap space. Although they are also real entities, we can't see or touch them.
If an object has no name, there is no way to access it directly. We need to access objects indirectly through object references.
The object is like a big balloon, but we can't catch it. The reference variable is a rope that can be used to tie a balloon.
Car car1; (1) Create a rope, a rope that has not been tied to any balloon;
Car car2; (2) He made another rope and didn't tie the balloon,
car1 = new Car(); (3) Tie a balloon to Car1;
car2 = car1 ; (4) Here, the copy behavior occurs. It should be noted that the object itself is not copied, but only the object reference is copied.
As a result, car2 also points to the object that car1 points to. The two ropes are tied to the same balloon
Difference between basic type and reference type:
Basic type
byte ,short,int ,long,float,double,char,Boolean
Its value is a number, a character, or a Boolean value.
Reference data type
Class, interface, array
Its value is the address of the object in the memory space, and the specific object exists in the heap.
Value passed in reference
When passing parameters in method calls in Java, there are two types of parameter passing:
Value transfer: (the shape parameter type is the basic data type): when calling a method, the actual parameter passes its value to the corresponding form parameter. The form parameter only initializes its own storage unit content with the value of the actual parameter, which is two different storage units. Therefore, the change of parameter value during method execution does not affect the value of the actual parameter.
Reference passing: (shape parameter type refers to reference data type parameter): also known as address passing. When calling a method, the actual parameter is an object, which means that the actual parameter can point to the same address as the formal parameter. During method execution, the operation on the formal parameter is actually the operation on the actual parameter. This result is retained after the method ends, so
The change of formal parameters in method execution will affect the actual parameters.
The basic type passes the data value itself. A reference type passes a reference to an object, not the object itself.
Static keyword
concept
Static is called static and can be used to modify class properties, methods, code blocks and internal classes.
Loads as the class loads
Superior object exists
Decorated members that are shared by all objects
It can be called directly by the class without creating an object
static property
Static attributes are shared by all objects of the class, that is, no matter how many objects are created, there is only one static attribute in memory.
public class Chinese{ String name;//Name non static attribute //static modifier Static String country;//Country static attribute }
static method can be called by object or directly by class name. It is recommended to call directly by class name
/** * @author The light wants to be quiet, but the wind stops. * static keyword */ public class Demo1 { /** * name Static member variable * age Amorphous member variable * sex Static member variable */ static String name; int age; static String sex; /** * Static method */ public static void zzh(){ System.out.println("Static method, which is loaded as the class is loaded"); } /** * The construction method is automatically executed when the object is created (that is, it appears the moment the object is created, so tm your objects appear, and you must follow it.) */ public Demo1(){ System.out.println("Nonparametric construction method"); } /** * Non static method * @return int */ public int num(){ return 666; } public static void main(String[] args) { //Static methods can be called directly with the class name Demo1.zzh(); Demo1. //Non static methods need to create an object and call it with an object Demo1 d = new Demo1(); d.num(); } }
Non static methods can call static variables, but static methods cannot directly call non static member variables
/** * @author The light wants to be quiet, but the wind stops. * static keyword */ public class Demo1 { /** * name Static member variable * age Amorphous member variable * sex Static member variable */ static String name; int age; static String sex; /** * Static method */ public static void zzh() { System.out.println(new Demo1().age); name = "zwj"; sex = "female"; System.out.println("Static method, which is loaded as the class is loaded"); } /** * The constructor is executed automatically when the object is created (that is, it appears the moment the object is created) */ public Demo1() { System.out.println("Nonparametric construction method,Is loaded as the object is created"); } /** * Non static method * * @return int */ public int num() { return 666; } public static void main(String[] args) { //Static methods can be called directly with class names, and static methods can be accessed without creating objects Demo1.zzh(); //Non static methods need to create an object and call it with an object Demo1 d = new Demo1(); d.num(); } }
Operation results:
In the static method, you can only access the static attribute of the class, not the non static attribute of the class. For non static attributes, you need to create objects and access them with objects. The static attribute is loaded first
Code block
A code block is declared in a class, similar to a method body (code block) without a name. The code is divided into instance blocks and static blocks
Instance block: called automatically each time an object is created
{
//Any Java code that conforms to the syntax
}
Static block: automatically called when a class is loaded, only once, regardless of whether to create an object.
static{
//Any Java code that conforms to the syntax
}
Class loading and execution
public class Demo2 { static { System.out.println("Static block, which is called automatically when the class is loaded, only once, regardless of whether to create an object"); } { System.out.println("Instance block: called automatically each time an object is created"); } public static void main(String[] args) { new Demo2(); new Demo2(); Demo2 d1 = new Demo2(); Demo2 d2 = new Demo2(); } }
Operation results:
package
Concept:
It's a folder
Function of package:
Avoid duplicate class names
Manage classes according to different functions
Control access
Naming conventions for packages:
In the package name, you can use Number to distinguish the level of the package; Package names are generally lowercase
The first level refers to the type of project, such as com, org, gov, etc
The second level refers to the name of the company where the project is developed or operated, such as oracle, sun, huawei, etc
The third level refers to the name of the project, such as bcms,oa,erp,cms, etc
The fourth level refers to the name of the project module, such as bean, action, exception, etc
Packages can better manage logically related classes and control access permissions between different packages
Class for importing external package, keyword "import"
Access modifier
The Java language has four permission access modifiers, which are in descending order:
Public: public permission modifies classes, properties, and methods. Can be accessed by any class
Protected: protected permission modifies attributes and methods. It can be accessed by classes in the same package. If it is not in the same package, it must be a subclass of this class.
(default): same package permission modifies classes, attributes and methods. Can only be accessed by classes in the same package.
Private: private permission modifies attributes and methods. Can only be accessed in this class