We know that there are two very important concepts in object-oriented programming speech, that is, classes and objects , where classes are abstractions of a batch of objects, and we can
To understand a class as a certain concept, the object is a concrete entity that exists.
The Java language that follows is an object-oriented programming language in which classes and objects are the core.
Creating a class in Java is mainly in the following format:
<span style= "FONT-SIZE:18PX;" >public class Teacher {}</span>
where modifiers, in addition to public, can use other modifiers, such as final, abstract, or completely omit these three modifiers.
The above is an empty class, when a class is generally defined, the most common members of a class are: constructors ,Field , and methods , where members with static adornments cannot access
A member that is not decorated with static; The constructor is used to construct an instance of the class, call the constructor with the new keyword, and return an instance of the class, if there is no
Defines the constructor, the system provides a default constructor for the class, and conversely, if the constructor is already defined, the system will no longer
For the constructor.
Then we look at the complete definition of a class:
<span style= "FONT-SIZE:18PX;" >public class Teacher {private int age=30;public Teacher () {}public void Printage () {System.out.println (age);}} </span>
Where field is: Private int age=30;
In addition to being decorated with public, you can omit the modifier, or you can use the following modifiers to decorate it: private, protected, static,
Final, where public, protected, private three can only use one of them, and static, final combination to modify the field.
The above field type is int, or it can be any data type allowed by the Java voice, including the base type and the reference type. This defines the int type, integral type,
The initial value is 30.
The above also defines a method Printage (), the modifier of the method is private, can also be omitted, you can also use public, protected, static,
Final, abstract, where public, protected, private three can have at most one, abstract and final can only use one, and
Static combined to modify the method.
The above method printage the return value type is void, that is, there is no return value. The return value type can be any data type allowed by the Java voice, including the base
This type and reference type, if the method return value type is declared, the method body must have a valid return statement.
The syntax format of the constructor for the teacher class is also decorated with modifiers, either omitted or using one of the public, protected, or private. Frame
The name of the builder must be exactly the same as the class name, and it is worth noting that the constructor does not define a return value type. If a return value type is defined, then it
is a method, not a constructor.
Next, use the New keyword to create an instance of this class:
<span style= "FONT-SIZE:18PX;" >public class Main {public static void Main (string[] args) {Teacher teacher=new Teacher (); Teacher.printage ();}} </span>
An instance teacher was created with the NEWI keyword, and the method Printage () of the class definition was called.
In the above code we are using teacher teacher=new teacher (), to create an instance of the class, this line of code produces two values, one is variable
Volume teacher, one is the teacher object.
Such as:
Heap Memory
When we create the teacher object, we need to have the corresponding memory to store the field of the teacher object. The above variables defined using classes are referred to as variable
That is, all classes are reference types. Since we have not seen the reference variable, the reference variable is actually stored in the stack memory to point to the actual
The teacher object. As shown in the following:
Stack memory heap
Stack in-memory reference variable teacher is not the field data that is really used to store the object, the field data of the object is actually placed in the heap memory, the reference variable is just
Points to the object in the heap memory.
According to the above, we know that when an object is successfully created, the object is stored in heap memory, and the Java program does not allow direct access to the heap memory
object, so you have a reference variable to manipulate the object by its reference.
Objects in the push memory can have multiple references, that is, reference variables that point to the same object.
Java also provides the This keyword, which is used to point to the object that called the method. Depending on where this is located, this is the default reference for the object in two
Situation:
1. The object that the constructor is initializing is referenced in the constructor
2. Refer to the object in the method that called the method
It is important to note that the this reference cannot be used in static decorated methods, that is, the static decorated method cannot access the normal form that does not use the static decoration.
Therefore, the Java syntax stipulates that static members cannot access non-static members directly.
Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/42651911 Emotional Control _
Java Object-oriented note 1