Object-oriented concepts (member variables, static variables, member methods, static methods, garbage collection mechanisms, overloading, and packages)

Source: Internet
Author: User

Object-oriented concepts (member variables, static variables, member methods, static methods, garbage collection mechanisms, overloading, and packages)

Differences between static variables and member variables:

1. member variables describe the characteristics of objects and are contained in objects. Different object member variables are independent of each other. Changes to the member variables of an object do not affect other objects.

Static variables are independent of objects and are shared by all objects. Changes to static variables will affect all objects.

2. member variables can only be passed through objects.. Static variables can be passed through the object.You can also use the class name.. Static variables are also called class variables.

3. member variables are stored in the heap space, and static variables are stored in the data segment.

 

Differences between static and member methods:

 

1. Before loading an object using static methods, you can use the object.And class name.. The member method can only pass through the object..

 

2. Static methods cannot directly access member variables and member methods. Member methods can access member variables and static variables

Garbage collection mechanism:

When a virtual machine recycles garbage, it recycles objects that are not pointed to by variables. Programmers cannot control this process. However, you can call System. gc () to speed up garbage collection.

 

Variables are divided by scope into three types:

 

1. Local variables are stored in the stack space and written in methods or statement blocks. When the block ends, the variable space is immediately released. Therefore, local variables cannot be used across methods or statement blocks.

 

2. member variables (attributes): stored in the heap, written outside the method, and in the class. It is generated when the object is generated and disappears when the object is destroyed.

 

3. Static variables (class variables): these variables are stored in the data segment, written outside the method, and in the class. Add the static modifier. Resident memory, will not disappear.

 

Concept of overload:

The overload occurs in the same class. The method names of the two methods are the same, and the parameter list is different (parameter type, parameter Number Parameter location). It is irrelevant to the return type. When calling a method. Determine the method to be executed based on the method parameters.

Package:

Is a folder. The package is used to define the package where the class is located.

The role of the package:

1. related classes can be organized

2. Information can be hidden. That is, after the public is removed, the class cannot be called by other packages.

If you want to access the classes in another package, you can import the classes through import. The classes in the following two packages can be directly used without importing them.

1. Classes in this package

2. classes in the java. lang Package

 

 

 

 

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.