If JAVA wants to make the class unable to be new, it can use private to change the class constructor to private. In this way, an error will be reported when the class is new. The private constructor

Source: Internet
Author: User

If JAVA wants to make the class unable to be new, it can use private to change the class constructor to private. In this way, an error will be reported when the class is new. The private constructor

If JAVA wants to make the class unable to be new, it can use private to change the constructor to private so that an error will be reported during the new class.

It is mainly used for static tools and static classes that can be called directly using the class name and static method without the need for new

Class D {private D () {} static void f () {System. out. println ("I Am a static method and do not need new. If you are new, I want to report an error to you. The class name is used directly. my name () can call me ");}/* If the class does not want to be new, you can write the constructor of the class that you do not want to be new to private. */public class Index {public static void main (String [] args) {// d a = new D (); // if new, the error D. f (); // call the static class }}

 

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.