Java tip [002]: Can classes be modified statically?

Source: Internet
Author: User

In fact, this is very definite. in the definition of a class, the static modifier cannot be used, but the reason is that there is a situation where the class can be defined as static, it is an internal class.

In the following definition, an internal class is implemented:

Package net. moon. insignificant. staticclass; </P> <p> public class staticclassdemo {</P> <p> Public static void main (string [] ARGs) {<br/> staticclassdemo. innerclass Ic = new staticclassdemo. innerclass (); <br/> IC. showmessage ("Hello, world"); <br/>}</P> <p> static class innerclass {<br/> Public void showmessage (string MSG) {<br/> system. out. println (MSG); <br/>}< br/>

The external class staticclassdemo is a common class. We can further modify it to change staticclassdemo to an abstract class. Then, we can include a default implementation in the abstract class. The Code is as follows:

Package net. moon. insignificant. staticclass; </P> <p> public abstract class staticclassdemo {</P> <p> Public static void main (string [] ARGs) {<br/> staticclassdemo. innerclass Ic = new staticclassdemo. innerclass (); <br/> IC. showmessage ("Hello, world"); <br/>}</P> <p> public abstract void showmessage (string MSG ); </P> <p> static class innerclass extends staticclassdemo {<br/> Public void showmessage (string MSG) {<br/> system. out. println (MSG); <br/>}< br/>

Further, we can also use this method to provide the default implementation for the interface. The Code is as follows:

Package net. moon. insignificant. staticclass; </P> <p> Public interface staticclassdemo {</P> <p> Public void showmessage (string MSG ); </P> <p> static class innerclass implements staticclassdemo {<br/> Public void showmessage (string MSG) {<br/> system. out. println (MSG); <br/>}</P> <p> Public static void main (string ARGs []) {<br/> staticclassdemo. innerclass Ic = new staticclassdemo. innerclass (); <br/> IC. showmessage ("Hello, world"); <br/>}< br/>

 

Next article: Java tip [003]: execution sequence during class initialization

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.