Java SE 23rd talk----the static keyword

Source: Internet
Author: User
Tags java se

1.static keyword "30 minutes to start teaching in 22-speaking video"

2.static decorated properties: No matter how many objects a class generates, all of these objects work together to use a unique static member variable, and one object modifies that static member variable, and the value of that static member variable for the other object changes as well. If a member variable is static, then we can use it in the same way as the class name. Member variable (Java recommends this way).

 Package com.cl.abstracttest;  Public class statictest {    publicstaticvoid  main (string[] args) {        new  mystatic ();         New mystatic ();         = ten;        System.out.println (mystatic2.a);    }} class mystatic{     int  A;}

The result is: 0

change int A to static int a the result becomes 10.

 package   Com.cl.abstracttest;  public  class   Statictest { static  void   main (string[] args) {MyS        Tatic mystatic  =  mystatic (); MYSTATIC.A  = 10; //  Use the class name directly. Member Variable mode              System.out.println (MYSTATIC.A); }}  class   mystatic{ static  int   A;}  

3.static Modification Method: The static method is called a method. For static methods, you can use the class name. Method name to access

 Packagecom.cl.staticandfinal; Public classStaticTest2 { Public Static voidMain (string[] args) {/*method One MyStatic2 s = new MyStatic2 (); S.output ();*/                //Method Twomystatic2.output ();//need to change method to static}}classmystatic2{ Public  Static voidoutput () {System.out.println ("Output"); }}

4. static methods can only inherit and cannot be overridden (override). subclasses cannot override parent class methods and can overload parent class methods

 Packagecom.cl.staticandfinal; Public classStaticTest3 { Public Static voidMain (string[] args) {m M=NewN ();    M.output (); }     }classm{ Public  Static voidoutput () {System.out.println (M); }}classNextendsm{ Public Static voidoutput () {System.out.println (N); }}
The output is: M
If you put
M m = new n (); instead n m = new n (); The result is n

5.final Keyword: Final can modify properties, methods, classes.

6.final Modified class: When a class is modified by final, it means that the class is a final state class and cannot be inherited.

Java SE 23rd talk----the static keyword

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.