Java2 Practical Tutorial Fourth Chapter

Source: Internet
Author: User

Bo Master Original reprint please specify address

    • Blog: http://www.cnblogs.com/13224ACMer/

1 Member variables

A variable declared by a declared variable is called a member variable and a domain variable, and the position of the member variable in the class is independent of the order in which it was written.

But one feature is the assignment of variables: member variables can have default values when they are defined, and domain variables do not have default values when they are defined, so you should pay special attention to the question of whether you need to assign a value when you define it.

The definition of variable name also has the requirements of the specification, if the variable is a word composition, then the best first letter capitalization, if it is two or even more than two words, it is best to start with the second word to capitalize the first letter

Actions on a variable in a class can only be manipulated in a method, such as a series of questions that need to be evaluated or assigned, and of course the assignment can also be assigned at the time of definition, but remember not to assign a value in addition to the method and define accidental assignment, which is not allowed in Java

2 Creating an object using a class to create an object needs to be divided into two parts,

1) First define a variable with a class

2) Use the new method to allocate memory for an object

For example, class Lader defines an object Lader

Lader Lader;

Lader=new Lader ();

After the new operator assigns a variable to the object, it returns a reference to the object variable

Variables assigned to objects we habitually call entities that are objects

3 garbage Collection

Sometimes we will assign an object to another object in the course of the operation, it is plain that a reference to a object is assigned to the B object, this is two objects common memory, a object's entity does not belong to any one variable,

Java has a so-called garbage collection mechanism that periodically detects whether an entity is no longer owned by any one object, and if such an entity is found, releases the memory occupied by the entity,

If you want the Java virtual machine to perform garbage collection operations immediately, you can call the GC () method with the System class

4

In the Java language, a class has a constructor method, but no destructor

5

If you need to compile all the Java files in a directory, you can go to the file and use the wildcard * to represent all the source file names to perform the function of compiling all the files.

For example, files under the c:p/directory

Then just need to execute c:p/>javac *.java

6 The relationship between a combination of objects and a class

If an object combines another object, the A object can use the method of the B object

There are several features by combining objects

(1) The method of combining objects is also called black box multiplexing, because the current object can only use the method of the combined object, and the details of the method of the combined object and the implementation of the algorithm is not known, so it is customary called black box multiplexing

(2) The current image can be changed at any time the contained objects, in fact, the object and the object contained in the weak coupling relationship

The relationship in the UML Middle school has learned 4 kinds of major relations, here the textbook only introduced two, then I will roughly summarize the two

If a member variable in Class A is an object declared with Class B, the relational relationship between A and B

Dependency: A and B are dependencies if the arguments of a method in Class A are objects that are declared by Class B or the data type Class B that a method returns.

7 instance members and class members

Definition: When declaring a member variable, if the keyword static is given a modifier called a class variable, otherwise called an instance variable, (class variable is also called a static variable, a variable)

The difference between the two variables:

Class variables: When a class has a class variable, if you use class new to create different objects, the class variables assigned to those objects collectively occupy a piece of memory, and the instance variables are each object's entities occupy one of their own objects of memory;

Regardless of whether the class variable is declared in the class, but when the Java program executes, the class's bytecode file is loaded into memory, and the class variable is automatically generated for a certain amount of memory, which is released when the program executes, but only when the instance member is defined.

Generates a space for it in memory, otherwise it will not generate space

8

Java2 Practical Tutorial Fourth Chapter

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.