JAVA Learning notes the sixth day __java

Source: Internet
Author: User

-----------Android Training, Java training, Java Learning Technology blog, look forward to communicating with you. ------------

Static keyword:

Used to decorate member variables and member functions

The modified member has the following characteristics: loaded with the class, takes precedence over the object existence, is shared by all objects, and can be directly called by the class name

Note: Static methods can only access static members, and static main methods cannot write This,super keywords, because static members are better than objects, so this is not possible in static methods, and the main function is static.

Method area-Shared area-data area

Class variables exist in the method area as the class is loaded, and the instance variable exists in the heap memory as the object is built. The life cycle of a class variable is the longest and disappears as the class disappears, and the life cycle of the instance variable disappears as the object disappears.

Main method:

The main function is a special function that can be invoked by the JVM as a portal to the program.

Public: Represents the maximum access permissions for this function.

Static: Represents the main function that already exists as the class is loaded

void: represents no specific return value for this function.

(String[]args): A parameter of a function whose type is an array of elements that are strings.

The main function is in a fixed format: JVM recognition

When the JVM invokes the main function, it passes in the new string[0];

When to use static:

When shared data occurs in an object, the data is decorated statically. The unique data in the object is defined as non-static in heap memory.

This functionality can be defined as static when there is no access to non-static data within the function (the object's unique data).

Static application-Tools class:

There are common features in every application. These functions can be extracted and encapsulated separately for reuse.

For example, although you can use these tool methods to create an Arraytool object, the array is manipulated.

1. Objects are used to encapsulate data, but Arraytool objects do not encapsulate unique data.

2. Each method of manipulating an array does not use the unique data in the Arraytool object.

At this point to consider, let the program more rigorous, is not required object. You can define the methods in Arraytool as static and invoke them directly from the class name.

After you define a method as static, you can make it easier to use, but the class can still be created by other programs.

In order to be more rigorous, forcing the class to not establish an object. You can privatize constructors. Can hide all hidden.

The production of help documents:

The Java specification is done through documentation comments.

/**

Gets the maximum value in an integer array

@auther John

@versionv1.1

*/

/**

function of

@paramarr receive an array

@return Returns a maximum value

*/

Javadoc–d (Direc) myhelp (Directory folder) –author–version Arrarytool.java

To generate a help document for a class, this class must be decorated by public.

The default constructor for a class is constructed with an empty argument, which is consistent with the owning class, and if the class is decorated by public, the default constructor also has the public modifier, and if the class is not decorated by public, the default constructor is not also decorated with public.

Static code block:

Format:

Static

{

Execution statements in a static code block

}

Static code block features: executes as the class is loaded, only once. Used to initialize a class

Constructs a code block that initializes an object and constructs a method for initializing the corresponding object.

The process of initializing an object

1. Because new uses the Person.class, it will first find the Person.class file and load it into memory

2. Execute the static code block in the class and, if so, initialize the Person.class class.

3. Open up space in heap memory and allocate memory address.

4. In heap memory that establishes the object's unique properties and initializes it by default.

5. Display initialization for properties

6. Construction code block initialization of the object

7. Initializes the corresponding constructor for the object.

8. Pay the memory address to the P variable in the stack memory.

Single-Example design pattern: Resolves a class that has only one object in memory.

How to guarantee unique objects:

1. In order to avoid the excessive creation of such objects by other programs, other programs are prohibited from establishing such objects.

2. In order for other programs to have access to the class object, you want to customize an object in this class

3. To facilitate access to custom objects by other programs, there are some ways of accessing them externally.

How do these three steps show up in code?

1. Privatize the constructor function.

2. Create a class object in the class.

3. Provides a way to get to the object

A Hungry man mode

Classsingle

{

Private single () {}

private static Single S = new single ();

public static single getinstance ()

{

Returns

}

}

How to describe things and how to describe them

When you need to make sure that the object is unique in memory, add the three steps above.

Another embodiment of a single example design pattern:

Lazy mode

Classsingle

{

Private single () {}

private static single s = null;

public static single getinstance ()

{

if (s==null)

s = new single ();

return s;

}

}

/**

* Demonstration of the mode of starvation in a single case

*@authorAdministrator

*

*/

Publicclass Eagersingleton

{

privatestatic Eagersingleton instance=New Eagersingleton ();

Private Eagersingleton ()

{

}

Public Static Eagersingleton getsingleinstance ()

{

return instance;

}

}

This is called starvation mode, and each object is initialized before it is used. This can lead to potential performance problems: If the object is large. Loading it into memory without using this object is a huge waste. In this case, we can improve on the above code, using a new design idea--deferred loading (lazy-load Singleton).

/**

* Demonstration of the lazy mode of single case mode

*@authorAdministrator

*

*/

Publicclass Lazysingleton {

privatestatic Lazysingleton instance;

Private Lazysingleton ()

{

}

Public Static Lazysingleton getsingleinstance ()

{

if (instance =null)

{

Instance =New Lazysingleton ();

}

return instance;

}

}


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.