Java inheritance and polymorphic-static keywords

Source: Internet
Author: User

1. What is the static keyword?

Static can be used in conjunction with variables, methods, and classes, called static variables, and statically methods. If you use static modifier variables or methods in a class, they can be accessed directly through the class, without the need to create an object of a class to access the members.

Instance:

Package com.java.javastatic;/** * Understand Java Static * @author Jeff * @date 2016/03/17 * Http://www.weixueyuan.net/vie W/6003.html */public class Demo01 {///define static variable apublic statics int a=1;//define variable bint b=2;//create static method sum () public static int sum (int X,int y) {return x+y;} public static void Main (string[] args) {//Access variable ASystem.out.println through class ("a=" +demo01.a);//Instantiate object Mydemo access variable bdemo01 Mydemo = New Demo01 (); System.out.println ("b=" +mydemo.b),//Through the class access method SumSystem.out.println ("X+y:" +sum (3,5));}}

Results:
A= 1
B= 2
X+y:8

2. Static memory allocation

Static variables belong to a class and do not belong to any independent object, so you can access static variables without creating an instance of the class. The result is that the compiler creates only one copy of the static variable for the entire class, that is, allocating only one memory space, although there are multiple instances, the instances share the memory. Instance variables are different, each time an object is created, the memory space is allocated once, the memory of different variables is independent from each other, and changing the instance variable of a object does not affect the B object.

Instance:

Package com.java.javastatic;/** * Static memory allocation * @author Jeff * 2016/4/3 * http://www.weixueyuan.net/view/6003.html */publi C class Demo02 {    static int i;    Int J;    public static void Main (string[] args) {        demo02 obj1 = new demo02 ();        obj1.i = ten;        OBJ1.J =;        Demo02 obj2 = new demo02 ();        System.out.println ("obj1.i=" + obj1.i + ", obj1.j=" + obj1.j);        System.out.println ("obj2.i=" + obj2.i + ", obj2.j=" + OBJ2.J);}    }

Results:
obj1.i=10, obj1.j=20
obj2.i=10, obj2.j=0

3. Summary of static variables and static methods:

Conclusion 1. The construction method does not allow the declaration as static;
Conclusion 2.1. Static methods can only access static variables
Conclusion 2.2. Static methods cannot access instance variables
Conclusion 3.1. Static methods can only invoke static methods;
Conclusion 3.2. Static methods are not able to invoke non-static methods;
Conclusion 4. The current object does not exist in the static method and therefore cannot be used, nor can it use super
Conclusion 5. Local variables cannot be modified with static
Conclusion 6.1. Static methods can be overloaded by static methods
Conclusion 6.2. Static methods can be overloaded by non-static methods
Conclusion 7.1 static methods, which can declare an object type, are generally not recommended for static methods because they cannot be accessed through the class
Conclusion 7.2 Non-static methods, which can be declared as object types, are generally recommended because instances can be used to access
Conclusion 8. Static methods can only be used in static classes
Conclusion 9.1. Static methods cannot be overridden by non-static methods
Conclusion 9.2 Static methods can be overridden by static methods
Conclusion 10 static methods are accessed directly through the class
Conclusion 11 if Access control permission is allowed, static variables and static methods can also be accessed through objects, but not recommended
Conclusion 12 static methods and type objects cannot be accessed through the class

Instance:

Package com.java.javastatic;/** * Static method and static method summary * @author Jeff * 2016/04/03 */public class demo03 {public static int a=1;int b=2;//Conclusion 1. The construction method does not allow the declaration to be static;//public static demo03 ()//{//Syste M.out.println ("I am a construction method");//}public int sum (int x,int y) {return x+y;} static method sum2, int type public static int sum2 (int x,int y) {//Conclusion 2.1. Static methods can only access static variables a=3;x=x+a;//conclusion 2.2. Static methods cannot access instance variables;//b=3;// Conclusion 3.1. Static methods can only invoke static methods; demo03.sum3 (1, 2);//Conclusion 3.2. Static methods cannot invoke non-static methods;//demo03.sum;//Conclusion 4. The current object does not exist in the static method and therefore cannot be used. Of course, you cannot use SUPER//THIS.SUM3 (1, 2); return x+y;} static method Sum3,int type public static int sum3 (int x,int y) {//Conclusion 5. The local variable cannot use the static modifier//static int C;return x+y;} Conclusion 6.1. Static methods can be overloaded with static methods public static int sum3 (int x) {return x;} Conclusion 6.2. Static methods can be overloaded with non-static methods public int sum3 (int x, int y, int z) {return x+y+z;} Conclusion 7.1 static methods, which can declare the object type, are generally not recommended because static methods cannot be accessed through the class public static Demo03 object () {demo03 d=new demo03 ();d. B=3;return D; Conclusion 7.2 Non-static methods, which can be declared as object types, are generally recommended because instances can be used to access publIC demo03 object2 () {demo03 d=new demo03 ();d. B=3;return D;} Conclusion 8. Static methods can only be used in static classes with public static class superclass {public static int getage (int.) {return age;} public static string GetName (string name) {return name;}} public static class ChildClass extends Superclass {//Conclusion 9.1. Static methods cannot be overridden by non-static methods//Public String GetName ( String name) {//return name;//}//Conclusion 9.2 static method can be overridden by static method public static S        Tring GetName (String name) {return name;} }public static void Main (string[] args) {//Conclusion 10 static method accesses the System.out.println directly through the Class ("X+y:" +demo03.sum2 (3,5));//Conclusion 11 Static variables and static methods can also be accessed through objects, such as access control permissions, but are not recommended demo03 de=new demo03 ();d e.sum2 e.a=4;//Conclusion 12 static methods and type objects that cannot be accessed through the class// Demo03.object;}}

4. Static Import

For static variables and static methods that are used frequently, you can import them statically.
Instance:

Package com.java.javastatic;//1. Via import static java.lang.system.*; Import it, and the next time you call OUT.PRINTLN () you will be able to import the static JAVA.LANG.SYSTEM.*;//2. Math.randomimport static in the import class java.lang.math.random;/** * Static import. For static variables and static methods that are used frequently, you can import them statically * @author Jeff * @date: 2016/4/4 * http://www.weixueyuan.net/view/6003.html */public class demo04 {public    static void Main (string[] args) {        //Output statement System.out.println (); Out is the static variable of the System class,        OUT.PR Intln ("generated a random number:" + random ());}    }

Java inheritance and polymorphic-static keywords

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.