JavaBean's understanding

Source: Internet
Author: User

Turn to knowing Tiansheng
Links: https://www.zhihu.com/question/19773379/answer/31625054
Source: Know
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

The Java language lacks attributes, events, and multiple inheritance capabilities. So, if you want to implement some of the common requirements of object-oriented programming in Java programs, you can only write a lot of glue code. Java beans are the idiomatic pattern or convention for writing this set of glue code. These conventions include GetXXX, Setxxx, Isxxx, Addxxxlistener, Xxxevent, and so on. Classes that follow these conventions can be used in several tools or libraries.

For example, if someone wants to implement a one-way linked list class in Java, this might be the case:
// compile into Java-int-list_1.0.jar  Public Final class javaintlist {  staticclass  node {    public  node next;     publicint  value;  }    Public Node head;    Public int size;}

In order to be able to quickly get the size of the list, the list size is cached in a size variable. Use the following:
New javaintlist (); System.out.println (mylist.size);

Javaintlist's author is very satisfied, so the open source of the Java-int-list Library version 1.0. The file name is Java-int-list_1.0.jar. After publishing, many users were attracted to use Java-int-list_1.0.jar.
One day, the author decides to save memory, do not cache the size variable, and change the code to this:
//compile into Java-int-list_2.0.jar Public Final classJavaintlist {Static Final classNode { PublicNode Next;  Public intvalue; }   PublicNode Head;  Public intGetSize () {Node n=Head; inti = 0;  while(n! =NULL) {n=N.next; I++; }    returni; }}

Then released the 2.0 version: Java-int-list_2.0.jar. After the release, the original Java-int-list_1.0.jar users have upgraded the version to 2.0. As soon as these users upgraded, they found that their programs were all broken, saying they could not find any size variables. So these users put the author to beat a meal, no longer dare to use the Java-int-list library.

The story tells us that if you don't want to be beaten to death, you have to stay backwards compatible. The Sun company also knows this truth when designing the Java language. Therefore, in the Java standard library, there will never be a public int size code, and must be written in the beginning:
Private int size;  Public int return size; }

Let the user start with GetSize, so that when the GetSize implementation is modified one day, it does not break backwards compatibility. This public int getsize () {return size;} Is the Java Bean.

=============================================== above turn to know ========================================================== ==

So I personally understand the JavaBean, this is just a specification rather than a certain kind or a technology, in order to achieve this specification, to meet the following four conditions.

1. All properties are private
2. Provide default construction method
3. Provide getter and setter
4. Implement Serializable interface

JavaBean's understanding

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.