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