Object-oriented static

Source: Internet
Author: User

Static

Static variable

Static variables are also known as static variables, except that the static variables are shared by all objects, only one copy in memory, and are initialized only when the class is first loaded. Instead of static variables, which are owned by an object, are initialized when the object is created, there are multiple copies, and the replicas owned by each object do not affect each other.
The initialization order of static member variables is initialized in the order in which they are defined.

static method
Static methods are commonly referred to as static methods, and because they can be accessed without relying on any object, there is no this for static methods because it is not attached to any object, and since there are no objects, this is not the case. And because of this feature, non-static member variables and non-static member methods of the class cannot be accessed in a static method, because non-static member methods/variables must be dependent on a specific object to be able to be called.
It is important to note that although non-static member methods and non-static member variables cannot be accessed in a static method, static member methods/variables can be accessed in non-static member methods. To give a simple example:

Class myobject{

priative static String strl-"Staticproperty";

Priative static str2-"property";

Public myobject{

}

public void Print1 () {

System.out.println (STR1);

System.out.println (STR2);

Print2 ();

}

}

Static code
When the class is loaded, the static code block executes, because the class is loaded only once, so the static code block executes only once.
Several lines of code enclosed with a pair of curly braces are called a block of code, and code blocks decorated with the static keyword are static blocks of code.

Single-row mode
The constructor method of a class is declared private by using the private modifier, so that the instance object cannot be created outside the class using the new keyword;
Creates an instance object of the class within the class and uses the static variable instance to refer to the object, since the variable should prohibit direct access to the outside world, and therefore uses the private adornment to declare it as a private member;
In order for an instance object of a class to be available outside the class, you need to define a static method, getinstance (), to return the class instance instance, because the method is static and can be accessed through the class name. Method Name.

Object-oriented static

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.