Application of Java Learning Note-static

Source: Internet
Author: User

Sometimes you want to define a class member so that its use is completely independent of any object of that class. Typically, a class member must be accessed through the object of its class, but it can create a member that can be used by itself without referencing a particular instance. Such a member can be created by adding the keyword static (static) before the member's declaration. If a member is declared static, it is able to be accessed before any object of its class is created, without having to reference any object. You can declare methods and variables as static. The most common example of a static member is main (). Because main () must be called when the program starts executing, it is declared as static. A variable that is declared static is called a statically variable or a class variable. Static variables can be referenced directly through the class name, or static variables can be referenced through the instance name, but it is preferable to use the former because the latter is prone to confusing static and generic variables. A static variable is associated with a class, and all instances of the class share a static variable. A method that is declared static is referred to as a stationary method or a class method. Static methods can call static methods directly, access static variables, but cannot directly access instance variables and instance methods. The This keyword cannot be used in a static method because the static method does not belong to any one instance. Static methods cannot be overridden by a static method of a quilt class. (from Baidu above) in order to better understand the static, I wrote a small example:

Package Study;

public class Teststatic {
int A;
Double b;
String C;
static int SS;
static void Printss () {
SYSTEM.OUT.PRINTLN (ss);
}
public double Add (int a,int b) {

PRINTSS (); You can also call a class variable here.

return a+b;
}
public static void Main (string[] args) {
TESTSTATIC.SS = 666;
TESTSTATIC.PRINTSS ();

System.out.println ("*******************");

Teststatic Hello = new Teststatic ();
HELLO.SS = 777;
TESTSTATIC.PRINTSS ();
}
}

The output is:

666
*******************
777

As you can see, we first refer directly to the static variable SS in Teststatic and assign it a value equal to 666; then we construct a new object Hello, if we modify the static variable SS equals 777 in the Hello object We print again the SS's worth time, has become 777.

One thing to keep in mind:

A class can use only classes, and objects can use objects, or methods of classes.

In a static method, you cannot invoke something inside a non-static method. In turn, a non-static method can invoke something inside the static.

Application of Java Learning Note-static

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.