Composition of the class

Source: Internet
Author: User

The composition of a class consists of the following parts:
Code Snippet
Dynamic Code Snippets
Static code

member Variables
Static Variables
Local Variables
Constants
member Methods
Static Methods
Construction Method
This (object)
Super (object)

To analyze the contents of the class in a more clear understanding of the composition of the Java class

Code Snippet:

A full curly brace is a piece of code that represents a separate memory space, and empty is a code

Dynamic Code snippet:
1, is a pair of curly braces {} Without any modification
2, that is, write log, or performance testing

Static code snippet:
1. A pair of curly braces with static modifier static{}, which is a code snippet that runs during the class load phase
2, write log, used to record the time of the class is called Data

member variables:
1, the values in each object are not the same, but can also be the same
2, Save the location: Heap memory, heap memory size is dynamically allocated
3, Assignment time: In general, before the instantiation is a null value, there is no assigned value
assign a value at instantiation, call the corresponding constructor to assign a value to the member variable, or do not assign a value, the compiler will give the default value. Assigning values by calling member variables can also
difference from local variables: Local variables must be declared to be assigned, or they will get an error.
is also a reference data type, when polymorphism occurs
4. Association relationship: The value of a member variable is a reference to another class object
5. Call: Object name. Member variable name. Class name cannot call member variable, non-static property cannot be called in static upper and lower

Static variables:
1, belong to the class level, each object is the same
2. Save location is static data area
3, it is best to assign value when declaring
4. Call: Class name. static variable name

Local Variables:
1, in the method of life variables are local variables, and life, you must assign value
2, local variable priority is greater than the priority of the member variable
3, the formal parameter list of the method is also a local variable
4, local variables and cyclic weight variables can not be duplicate name
5. Dependency: The value of a local variable is a reference to another class object

constants:
1, Declaration: public static final data type Capital _ = value;
2, the value will not change throughout the software life cycle
3, in the interface, PSF can be omitted
4. Call: Class name. Constant Name

member Methods:
1, no static modification method called member method
2. Call: Object name. Member Method name (argument list);
3, Polymorphism: Method of life, the formal parameter list is the parent class or an excuse
when called, a reference to the subclass object is passed

static method:
1, the method of static modification
2. Call: Class name. static method Name (argument list);
3. Save in the static code area
4. Non-static member properties can no longer be called in a static method

Construction Method:
1, used in the class instantiation, the method of creating the current class object (instantiating the current class object)
2, the default is: public, static, no return value, the name and class name of the same
3, in instantiation, is commonly used to instantiate member variables
4. Note: Use in single-case mode
constructed overloads, assigning values to different member variables
can not be inherited by replication, but can be called, super ()
There is no construction method in the interface, because the interface is implemented with a class, no constructor is required, and the interface cannot be instantiated
there are constructor methods in the abstract class for subclasses to instantiate the parent class super ()

This (object):
1 prefix the member variable and copy
2 in the constructor method, the overload calls the other constructor methods in this class, which must be written in the first row of the constructor method when the this () overload is called
3 is used to represent the object of this class, which holds the address of this class, which is located in the first memory slice in heap memory
4 in heap memory, we have the memory address of the first memory slice in the heap memory as the memory address of the entire object, and named this

Super (object):
1 Super Represents the parent type feature in the current subclass object
2 The constructor method overload of a subclass calls the constructor of the parent class and must be written in the first row
3 Super () and this () cannot recur
4 Super can be used to invoke the parent class's member properties and member methods


in the above description, there are two words that appear at a high frequency: called. In fact, the main function of the class is: code reuse

the 6-in relationship between class objects:
1 Inheritance
2 Implementation
3 Dependency: A local variable is a reference to another class object
4 Association: A member variable is a reference to another class object
5 Aggregation: An element in a collection in a class is a reference to another class object
6 Combination

Composition of the class

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.