Java static variables and static method Inheritance Mechanism

Source: Internet
Author: User

Suppose, what if the inheritance of 'static variable' could be like what I thought was naive?

[Java]
Class Person {
Public static String name = "person ";
Public static String name (){
Return name;
}
 
}
 
Class Person2 extends Person {
}
 
Class Person3 extends Person {
 
}


Both Person2 and Person3 are inherited from Person.

Then I hope that:

[Java]
Public class Test {
Public static void main (String [] args ){
Person2.name = "person2 ";
Person3.name = "person3"
 
Person. name (); // you want to print "person"
Person2.name (); // you want to print "person2"
Person3.name (); // you want to print "person3"
 
}
 
 
}


However, the following summary is printed:

Static variables and static methods are completely specific to a class. Static variables that inherit access from the parent class are also its father's. You can declare that your static variables overwrite the static variables of the parent class, but if you use a static method that inherits from the parent class to obtain the static variables, what you finally get is the static variable of the parent class.

This is a complete design !!!

Inheritance should be a copy that inherits to the parent class!

Why do I strongly want to inherit copy instead of a simple reference?

It makes some declarative Syntax of java impossible.

The following are all based on static variables and method inheritance, and a new copy assumption will be obtained in the subclass (this is not actually supported ), never think that the following example is correct

For example, when designing a model class:

[Java]
Public class Document {
Protected static String storeIn = "";
Public static void storeIn (String name ){
StoreIn = name;
}
 
Public static String storeIn (){
Return storeIn;
}
.......
}
 
Public class Person extends Document {
Static {
StoreIn ("persons ");
HasMany ("addresses", new Options (map (
Options. n_kclass, Address. class,
Options. n_foreignKey, "person_id"
)));
}
}


Each subclass can store its own configuration information through static variables. By inheriting from the parent class, you can avoid declaring these static variables and methods each time. Is there any clue? Static variables can be used as a thing similar to Annotation and are simpler than Annotation. What is Annotation, that is, to annotate a class (domain, method, etc.), right?

The above Person statement is equivalent:

[Java]
@ StoreIn ("persons ")
Public class Person extends Document {
@ HasMany (Options. n_kclass = Address. class)
Private List addresses;
.....
}


At first glance, I felt almost the same. However, Annotation has many restrictions, such as the inability to store complex types of information. And once it is written, it cannot be changed. What if static variables are used?

How can I obtain the storeIn information?

[Java]
Person. storeIn ();
 
// Method of annotation
Person. getClass. getAnnotations ()
... Blablalba... a lot.


Then, when the program is running, it can completely modify the value of the static variable. If you think it is not safe, you can use final to restrict it.

In fact, I have mentioned so much about it. Another idea is that static variables are inherited from the parent class. You do not need to write them one by one in sub-classes like Annotation. You can call a simple method to set the annotation information of a class. How cool.

But now it is impossible because of the poor static inheritance Design of Java I just mentioned. Otherwise, do you still need Annotation? Yes! But it can be used only as a supplement to static variables.

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.