JAVA Note Seven

Source: Internet
Author: User

Java Note Seven

This: Represents the object of this class, this represents a reference to the object to which the function belongs.
Simply put: that object is calling the function where this is, this represents the object
Static: Statics
Usage: is a modifier used to decorate members (member variables, member functions)
When a member is statically modified, it has a more calling method, except that it can be called by the object.
It can also be called directly by the class name. Class name. Static member
Static features
1. Load with class loading
2. Precedence over the existence of the object
3. Shared by all objects
4. Can be called directly by the class name
The difference between an instance variable and a class variable:
1. Storage location.
Class variables exist in the method area as the classes are loaded.
Instance variables exist in heap memory as the object is established.
2. Life cycle:
The class variable has the longest life cycle and disappears as the class disappears
Instance variable life cycle disappears as the object disappears
When do I use static?
We have to do it in two ways.
Because statically decorated content is used with member variables and member functions
When do you define static variables (class variables)?
When shared data appears in an object, the data is decorated statically.
Unique data in the object to be defined as non-static in heap memory
When do you define a class function?
When there is no access to non-static data (unique data for the object) within the function,
Then the function is defined as static.
Class peson{
String name;
public void Show () {
System.out.println ("haha");
}
}
class{
public static void Main (String[]args) {
Person p = new person ();
P.show ();
}
}
}
static applications
Class Demo
{
public static void Main () {
Int[] arr={3,4,1,8};
int Max =getmax (arr);
System.out.println ("max=" +max);
}
public static int Getmax (int[] arr) {
int max = 0;
for (int i =1;max<arr.length;x++) {
if (Arr[x]>arr[max]) {
Max=x;
}
}
return Arr[max]

}
}
Design Patterns: The most effective way to solve a class of problems
Singleton design pattern: Resolves a class that occupies only one object in memory
Want to ensure that the object is unique
1, in order to avoid other programs too many to establish such objects. Prevent other programs from establishing this class object first.
2, and in order for other programs to have access to the class object, in this class, customize an Object
3, in order to facilitate other programs on the custom object access, can provide some external access.
How do these three steps manifest themselves in code?
1, privatization of the constructor function
2, create an object of this class in the class
3, provides a way to get the object

Class single
{
Private single () {
private static Single S = new single ();

public static single getinstance () {

return s;
}
}

}

Class Singledemo
{
public static void Main (String[]args) {

Single SS = Single.getinstance ();
}
}

JAVA Note Seven

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.