Java basics-Definitions of classes, member variables, methods, creation and use of objects

Source: Internet
Author: User

A. JAVA definition of Class

Java has a class keyword that defines a category, followed by a custom class name.  As defined here, the person class, which defines a person class with class person, then defines the member variables (that is, attributes) and methods that the person class should have, such as the int ID and int defined here, in the class body of the person class. Age this two member variable, or attribute, this ID represents the person's identity card number, the person should have this attribute, ages is the person's aging, this is also the person should have. This defines the attributes that two people should have in the person class, followed by the definition method, which defines three methods, namely Getage (), setage (int i), and GetID (), respectively, to obtain the age of the person, set the age of the person, get the ID of the person, The Getage () method gets the age of the person and returns the value that is obtained, so that the return ages statement is used, and the GetId () method also uses the return ID statement to return the value of the obtained ID.

Two. member Variables

  in the Any variables in Java should be declared first, then assigned, and then used. There is one important difference between a member variable and a local variable : If the member variable is declared in the class without initialization, then Java defaults to initializing it, and the local variable Java does not initialize it by default. So declaring a local variable inside a method will make an error if it is not initialized. The default initialization is mostly the 0,boolean type is false, the reference type is NULL, if you do not remember how much Java initializes the default initialization of member variables, then do so, define a member variable, do not initialize it, and then print the member variable directly, The printed result is the default initialized value of java.

Third, the basic concept of Java object-oriented-reference

There is a huge difference between the reference type and the base type , when declaring an int i=0, the system immediately allocates a memory space for the I (allocating a small area in the stack memory for the number 0) with a value of 0. After using the name I will immediately be able to access the value inside this memory space, this is the basic data type, so the underlying type is only a piece of memory . Types other than the underlying type are all called reference types, and we define a mouse m, which is a reference type of data. What are the important characteristics of a reference type--the reference type accounts for 2 blocks of memory . After we have defined this class, we need to instantiate the object of the class with the new keyword, which is actually creating an object out of which to use the object.

How to classify and object in memory Central

The class is a static concept and is located inside the code area. The object is new, it is in heap memory, why is the object in heap memory? Because the heap memory is used to dynamically allocate memory, only in the run in the new object in the heap memory inside, that object exactly how many, this thing you do not know, you have no way to know in advance, so you have no way to allocate memory to this object in advance, you can only allocate it during the run. What do you mean, run period? Knocking Javac This command is during compilation, when the compilation is completed and then the Java command is hit, that is the run time. Only during the run can you understand how much space this object allocates to it, so put it in heap memory, heap memory is larger, and dynamically allocate memory for it. If this object is not used, then it is garbage, then wait for the garbage collector to collect it back, freeing up the occupied memory.

Remember, as soon as a reference is mentioned, the reference in the brain immediately refers to a small chunk of memory pointing to a chunk of memory.

Iv. creation and use of objects

V. Relationship of classes and objects

Parsing the relationships of classes and objects in memory

Suppose there is a class C, we define a Class C, and then define two member variables in this class: int i and Int J. After defining these two member variables, we wrote a Main () method (public static void main (strng[] args), and the program began to execute. The first sentence we wrote C C1 = new C (), the code of this sentence is that we create an object in the heap memory, but also create a reference object of this object c1,c1 in the stack memory, C1 the reference object to a large chunk of memory in the heap, this large memory contains the new object. Here we are generally new out of two objects C1 and C2, of course, in fact, strictly speaking, C1 and C2 are called object references, and sometimes, for short, new comes out of two objects, C1 and C2. You're going to have two pieces of memory coming up in your head, C1 pointing to a piece, c2 pointing to a piece. Local variables are allocated in the stack memory, the main method inside the C1 and C2 are local variables, so in the stack is allocated two small pieces of memory out, a piece is c1, a piece is C2, C1 this piece of memory contains a value, or is loaded with an address, this address is what, we do not know, We only know that based on this value we can find a new C in the class of the object, and in this object has its own member variables I and J, the inside of the two small pieces of memory is used to install the values of I and J, because each object has its own different member variable values, So C1 points to the memory inside a small block of memory, each small block of memory is loaded with the object's member variables (or called attributes). As the first small block here contains the value of I, the second small block contains the value of J, so when we go to the first small block containing the member variables, we should write: c1.i, so I get the value of I, C1.J, so I get the value of J. In the same vein, the C2 object also points to another object in the class of new C, which also has member variables I and J, except that the values of I and J in the object pointed to by C1 are different. To access the member variables of this object, it is also necessary to c2.i,c2.j such access.

Vi. Construction Methods

In object-oriented, there is a special method called the constructor method. The constructor method is used to create a new object, combined with new, to create a new object using the new+ construction method. When you're new to something, you actually call a constructor that constructs a new object, so it's called a construction method, and a method called a constructor is used to construct a new object.

The construction method is special, and the name of the constructor must be exactly the same as the name of the class, including case, and no return value. As originally defined by a person class, in the class declared two member variable ID and age, this time you can then define a constructor method for the person class person (int n,int i), the method whose name and class name are exactly the same, and there is no return value, That is, the return type modifier of any method cannot be written before this method, and even void cannot be written.

Examples of construction methods:

1  Public classPerson {2     intId//within the person category, define two member variable IDs and age,3     intage=20;//to the member variable, age assigns an initial value of4 5     /**Here's a way to construct the person class.6 * The rules for constructing methods are simple, and the class names are exactly the same, not at all wrong, including capitalization. 7 * and no return value, cannot write void before it is decorated8      * @param_id9      * @param_ageTen      */ One      PublicPerson (int_ID,int_age) { AID =_id; -Age =_age; -     } the}

When the construction method is written, it is used in combination with new, and new is constructed to create a novel object, so the constructor is actually called when new. Only this constructor method is called to construct a new object. For example:

 Public Static void Main (string[] args) {2     New // call the person constructor to create a new object and assign an initial value of 3} to the member variable of the object.

The following is a memory analysis when invoking the person constructor method inside the Main method:

when the method call is complete, the space allocated for it in the stack disappears, that is, the memory space allocated to it when the method is called is freed , so after the constructor person call is completed, the two small pieces of memory allocated in the stack memory _id and _age automatically disappear. This gives them the space they occupy and allows other methods to occupy them. The new object is always left in the heap memory.

declares a class whose constructor (constructor) is not specified in the class, and the compiler automatically adds a shape such as the class name () {} to the class . 's constructor .

Such as:

class  point{// Declaration of a class here does not give it a constructor method. 2     int  x; 3     int  y; 4}

But in the main method we can use this:

 Public Static void Main (string[] args) {2     point  newpoint  (); 9 ·

It is possible to write this in this way, and when the class is not given a constructor method, the system will default to this class with an empty construction method such as point () {}. That's why you can use it in the Main method
Point P = new Point (); In fact, you are invoking the constructor of the point () {}, which is added by default by the compiler, which, by default, sets the member variable x and y initial values in the class to 0. It is because the system adds a constructor to it by default, so it can be called in the Main method. But remember that once you assign a constructor to the class, the system will not add a constructor to the class .

Java basics-Definitions of classes, member variables, methods, creation and use of objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.