The Static keyword in Java

Source: Internet
Author: User

Static is the meaning of statics.

Static keyword:

1.static modified variables are called "Static variables".

The 2.static retouching method is called a "static method".

3.static can also define static statement blocks.

The following example shows a static statement block that is statically defined

Static statement blocks defined by static are executed at the class load stage and executed only once, and are executed in a top-down order

public class statictest01{//Static statement block Static{system.out.println ("1");} Static{system.out.println ("2");} Static{system.out.println ("3");} Static{system.out.println ("4");}  Ingress public static void main (string[] args) {System.out.println ("main execute! 1 ");  System.out.println ("Main execute! 2 ");}} 1 2 3 4 main execute!1 main excute!2

Static methods cannot call member methods directly, member variables, and use the This keyword. Because the static method accesses the "class name." Way.

About Instance statement blocks

/* The instance statement block is executed once each time the constructor method is called. Instance Statement block execution order is also top-down. */public class statictest02{//Static statement block Static{system.out.println ("A");} Instance statement block {System.out.println ("1");} {System.out.println ("2");} {System.out.println ("3");} Construction Method StaticTest02 () {System.out.println ("StaticTest02 parameterless construction Execution! ");} Ingress Public static void main (String[] args) {//Call constructor Method new statictest02 ();new  StaticTest02 (); new statictest02 ();}}  1 2 3 4 statictest02-no-parameter construction execution!  // 1 2 3 4 statictest02-no-parameter construction execution!  // 1 2 3 4 STATICTEST02 constructs that do not have parameters are called "static variables" that perform!   /*static modifications. Variable categories: 1. Local variables 2. member variables (instance variables, non-static variables) 3. Static variables (method areas) When variables are declared as static variables? If this property has all objects and the value of this property is the same, then the property is declared as a static property. Member Variable: Initializes the Java object when it is created. Static variable: Assigns a value at the class load stage and assigns only one value at a time. */public class statictest04{public static void main (String[] args) {Animal  A1 = new animal ("AAA", 10); Animal a2 = new animal ("BBB", 11);//How to access (class name.) System.out.prIntln (Animal.type);//You can also use   references. System.out.println (A1.type); animal a3 = null; System.out.println (a3.type);  //static variable The underlying access must use the    class name .  is independent of the object, and no null pointer exception occurs. }}//Abstract real-world "terrestrial" animals class animal{//field//member variables (one copy of the object.) Member variables are initialized when the object is created and stored in each object in the heap. string name;int age;//static variables, which are stored in the method area.//All Java objects share this copy. So static variables are class-level, using the "class name." The way to visit .static string type =  "Lu Sheng";//constructoranimal (string name,int age) { This.name = name;this.age = age;}}


This article is from the "Gaogaozi" blog, make sure to keep this source http://hangtiangazi.blog.51cto.com/8584103/1661136

The Static keyword in Java

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.