Java Basics (2)--use of the keyword static

Source: Internet
Author: User

When declaring properties, methods, and inner classes in a Java class, you can use the keyword static as a modifier, and the properties and methods of the static tag can be shared by the entire class, so the static decorated property is called a class member or a class method The static modification method is called a class method or a static method;

A static modified statement block is executed once when it is loaded into the JVM in the class to which it belongs, and is typically used for one-time initialization, whereas a non-static statement block is executed once for each object created. Its run time is the object allocation space and the instance default and the display after initialization and execution of the constructor method in the body before the statement;

/**
*static operation use
* @author lihaiming
*email:[email protected]
*/

public class teststatic{
Member variables, each of which has one copy in each object;
private int i=23;
Class variable, there is only one copy in the global;
private static int j=32;

public static void Method1 () {
I=200: Wrong! Variables cannot be called directly in a static method
New Teststatic (). i=90;
j=34;
int k=2;
}
public static void Main (string[] args) {
Teststatic ts = new teststatic ();
System.out.println ("ts.i=" +ts.i);
System.out.println ("j=" +j);
System.out.println ("ts.j=" +TS.J);
System.out.println ("teststatic.j=" +TESTSTATIC.J);
System.out.println ("------------------");

Teststatic ts1 = new Teststatic ();
Teststatic ts2 = new Teststatic ();

ts1.i=100;
ts2.j=200;

System.out.println ("j=" +j);
System.out.println ("ts.j=" +TS.J);
System.out.println ("ts.j=" +TS1.J);
System.out.println ("ts2.j=" +TS2.J);

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

System.out.println ("ts.i=" +ts.i);
System.out.println ("ts1.i=" +ts1.i);
System.out.println ("ts2.i=" +ts2.i);
System.out.println ("------------------");
Method1 ();
System.out.println ("ts.i=" +ts.i);
System.out.println ("j=" +j);
li.i=100;
Li L1 = new Li ();
Li L2 = new Li ();



}

}


Class li{
static int i=20;
int j=30;
Executes a non-static initialization statement block for each object that is created;
{

j=50;
System.out.println ("i=" +i);
System.out.println ("j=" +j);
}
Call Static First
static{
System.out.println ("i=" +i);
System.out.println ("A static!");
}
}

Program results:

Ts.i=23
J=32
Ts.j=32
Teststatic.j=32
------------------
j=200
ts.j=200
ts.j=200
ts2.j=200
------------------
Ts.i=23
ts1.i=100
Ts2.i=23
------------------
Ts.i=23
J=34
I=20
A static!
i=100
J=50
i=100
J=50

Java Basics (2)--use of the keyword 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.