Summary of static classes and classes and classes in the Java language

Source: Internet
Author: User

First, the Static class

1. Definition of static class

Static, which can be used to modify the properties or methods of a class, is known as statics.

If a property of a class, regardless of how many objects are created, has only one storage space for the property, then this property should be modified with static, which is called a static property by the static decorated property.

The static property can be invoked using an object or directly with the class name.

The static properties of the class are shared by the object, that is, no matter how many objects are created, the static property has only one in memory.

public class employee{

private String name;--------------------------Non-static class

private double salary;------------------------non-static class

Private static int count;-----------------------Static class

}

2. Static method definition

If a method does not need to be bound to a particular object, the method can use the static modifier, which is called a static class method.

    access Rights   static modifier  return value type Method Name

     Public Static void Sellticket (){           

System.out.println ("All fire tickets are sold at 12306!") ")

}

3, the static method can use the object call, or directly with the class name to call the proposed class name directly call

4. When to use the static method

If a method is not related to an instance, that is, regardless of whether that object calls the method, performs the same operation, and does not have a relationship with the object, it should be defined as a static method. You do not need to create an object after you use this method.

For example: Math in the API is a static method because the math is not related to the Math object itself and is called directly using the class.

Any method can call the static method directly;

Static methods cannot call static methods directly, need to create objects, call non-static methods with object names

5. Static block

The properties of static blocks and static and the properties of the methods are the same, the static code block is represented by a statically decorated block of code, and the code block executes when the Java Virtual machine loads the class.

The difference is that the syntax is different.

Static block syntax: Adding the keyword static directly to the code block becomes a static block.

Static use

1), static blocks are executed only once when the class is loaded, no matter how many objects are created.

2), if a class load, always have to do something beforehand, you can put in a static block, for example, read a property file, make some general configuration, write some logs and so on.

3), a class can have more than one static block, in order to execute.

Second, final

1. Final for defining declaration properties, methods, and classes

Property: The definition must be assigned directly or assigned in the constructor method, and cannot be modified at a later stage.

Method: The definition must have implementation code, and the subclass cannot be inherited.

Class: cannot be defined as an abstract class or interface and cannot be inherited.

2. Final modifier properties

When the final property is modified, the properties of the base data type become constants and cannot be modified

Constants in some mathematical theorems often use final modifiers

3. Assignment of final attribute

Assign values at the same time when declaring, often with static

Do not assign value at declaration, must be assigned in constructor method

General principle: Ensure that when each object is created, the final attribute learns that it is determined

Cases:

4. Final modification parameters

The final keyword is added before the method parameter in order to prevent the data from being modified in the method body.

Iii. the relationship between class and class

1. Association relationship

1), Correlation relationship definition

The link between the object and the object. In Java, the code representation of an association relationship is the existence of a class of attribute types that are another class.

That is, "have" relationship: "Has-a".

2), the direction of the association relationship

One-Way Association: Class A association Class B.

Two-Way Association: Class A Association Class B, Class B association Class A;

  

3), the Multiplicity of association relations

One-to-one association: A student can only study in a class.

    

One-to-many association: a student can participate in multiple classes to learn

Solution: Collections and Arrays

2. Dependency relationship

1), dependency definitions (USE-A)

Refers to a class A used to another class B

2), the characteristics of the dependency relationship

This relationship is accidental, temporary, and very weak, but changes in class B affect Class A.

3), reliance on specific performance

At the code level, the dependency behaves as a Class B as a parameter by Class A in a method

method is used in the

    

3. The difference between relevance and dependence

Associations are "has" relationships, and dependencies are "use" relationships

Class A relates to Class B, which refers to the existence of a class B object as an attribute of Class A, known as the "has" relationship.

Class A relies on Class B, which refers to the existence of a B object as a method parameter of Class A, called the "use" relationship.

Different life cycle

If Class A associates Class B, an object of Class A is instantiated, and the Class B object is initialized until the class A object is destroyed, and the associated Class B object is destroyed. That is, if a class A object exists, the class B object exists.

If Class A relies on Class B, then only when the Class A object is called to the appropriate method, the Class B object is created temporarily, the method execution ends, and the Class B object is recycled, and the dependency between Class A and category B is a transient relationship.

  

Summary of static classes and classes and classes in the Java language

Related Article

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.