Java constructor modifier

Source: Internet
Author: User

Today saw the sword refers to the second chapter of the offer-the basic knowledge of the interview needs, for one of the C # interview code, more interested in thinking about whether to change to Java, the results did not change, there is a problem, this article is to summarize this, the following is C # and Java code, but in the eclipse environment , the Java code is so rewritten that the compiler reminds the constructor that there is a problem with the reason that the problem is caused by the access modifier public and the special modifier static before the Java constructor.

Java constructors, which can be decorated with access modifiers and not decorated with special modifiers; (tested by the compiler)

Access modifiers: public (most commonly used, anywhere else in the class can be accessed)

Protected (can be accessed in the same package by the quilt Class)

Do not write anything, "" (Can be accessed by the class in the same package)

Private (in a frequently used singleton mode)

Special modifiers: static (Static) final (final) abstract (abstract) synchronized (synchronous) None of these can be used before the constructor

1 classa{2       PublicA (String text) {3 Console.writeln (text);4      }5 }6 7 classB () {8      StaticA a1=NewA"A1");9A a2=NewA"A2");Ten  One      StaticB () { Aa1=NewA"A3"); -      } -       the       PublicB () { -A2=NewA"A4"); -      } - } + classprogram{ -B b=NewB (); +}
View Code
1  Public classA {2      PublicA (String aStr) {3System.out.println ("A constructor initialization:" +aStr);4     }5 }6 7 classB {8     StaticA A =NewA ("A1");9A AA =NewA ("A2");Ten  One     StaticB () { AA =NewA ("A3"); -     } -  the      PublicB () { -AA =NewA ("A4"); -     } -      Public Static voidMain (string[] args) { +B b=NewB (); -     } +}
java View Code

Java constructor modifier

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.