TRUE and Boolean. FALSE will be used instead of creating a new one. It's just a perk in Java, really. I'm not sure why you would want to make your own wrapper class, either, considering that any primitive data type already has a predefined wrapper This feature was added to Java 1.
This kind of magic available only to primitive values and correspondent wrappers. And you can't do it yourself in Java. If you still want it, than go for Scala - it's great! Namely you can use feature called implicit conversions. Here is small example for your case:. No, it's just compiler magic; it treats these as special cases known as autoboxing. See e. It's called autoboxing and is basically just something the compiler does for you. It was added only in Java 5, before that you did have to use new Boolean true , or better Boolean.
And no, you cannot have it for your own classes, it's only done for the primitive wrapper classes. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How does the Java Boolean wrapper class get instantiated?
Ask Question. Asked 10 years, 9 months ago. Active 10 years, 9 months ago. Viewed 8k times. I now have an object that holds the value "true".
Wrappers simply allow primitive data types to be used as full-fledged objects in the language. They all offer such methods as equals , which checks if the value of the specified objects are equivalent, and compareTo , which performs a signed comparison of two objects.
Objects instantiated from the wrapper classes are immutable; that is, once created, they cannot be modified. The discussion of wrapper classes starts off this chapter as a bridge to discussing strings and arrays.
Previous page. Table of content. Next page. Note There is one additional wrapper class, called void. Note Objects instantiated from the wrapper classes are immutable; that is, once created, they cannot be modified. Authors: Eben Hewitt. B 's constructor and instance method show that the type of the receiver parameter may be denoted with a qualified TypeName like any other type; but that the name of the receiver parameter in an inner class's constructor must use the simple name of the enclosing class.
The signature of a method m 1 is a subsignature of the signature of a method m 2 if either:. Two method signatures m 1 and m 2 are override-equivalent iff either m 1 is a subsignature of m 2 or m 2 is a subsignature of m 1. It is a compile-time error to declare two methods with override-equivalent signatures in a class.
This program causes a compile-time error because it declares two move methods with the same and hence, override-equivalent signature. This is an error even though one of the declarations is abstract. The notion of subsignature is designed to express a relationship between two methods whose signatures are not identical, but in which one may override the other.
Specifically, it allows a method whose signature does not use generic types to override any generified version of that method. This is important so that library designers may freely generify methods independently of clients that define subclasses or subinterfaces of the library. Now, assume this code was written before the introduction of generics, and now the author of class CollectionConverter decides to generify the code, thus:.
Without special dispensation, Overrider. Instead, the code would be illegal. This would significantly inhibit the use of generics, since library writers would hesitate to migrate existing code. It is a compile-time error if a method declaration that contains the keyword abstract also contains any one of the keywords private , static , final , native , strictfp , or synchronized.
It is a compile-time error if a method declaration that contains the keyword native also contains strictfp. If two or more distinct method modifiers appear in a method declaration, it is customary, though not required, that they appear in the order consistent with that shown above in the production for MethodModifier.
A method that is not abstract may be referred to as a concrete method. An abstract class can override an abstract method by providing another abstract method declaration. This can provide a place to put a documentation comment, to refine the return type, or to declare that the set of checked exceptions that can be thrown by that method, when it is implemented by its subclasses, is to be more limited. An instance method that is not abstract can be overridden by an abstract method.
The overriding declaration of method get in class InfiniteBuffer states that method get in any subclass of InfiniteBuffer never throws a BufferEmpty exception, putatively because it generates the data in the buffer, and thus can never run out of data. We can declare an abstract class Point that requires its subclasses to implement toString if they are to be complete, instantiable classes:. This abstract declaration of toString overrides the non- abstract toString method of the class Object.
Object is the implicit direct superclass of class Point. Adding the code:. Method toString of class Object can be made available to class ColoredPoint only if class Point explicitly makes it available through some other method, as in:.
A method that is declared static is called a class method. A class method is always invoked without reference to a particular object. A method that is not declared static is called an instance method , and sometimes called a non- static method. An instance method is always invoked with respect to an object, which becomes the current object to which the keywords this and super refer during execution of the method body.
A method can be declared final to prevent subclasses from overriding or hiding it. It is a compile-time error to attempt to override or hide a final method.
At run time, a machine-code generator or optimizer can "inline" the body of a final method, replacing an invocation of the method with the code in its body. The inlining process must preserve the semantics of the method invocation. In particular, if the target of an instance method invocation is null , then a NullPointerException must be thrown even if the method is inlined. A Java compiler must ensure that the exception will be thrown at the correct point, so that the actual arguments to the method will be seen to have been evaluated in the correct order prior to the method invocation.
Inlining the method move of class Point in method main would transform the for loop to the form:. Such inlining cannot be done at compile time unless it can be guaranteed that Test and Point will always be recompiled together, so that whenever Point - and specifically its move method - changes, the code for Test. A method that is native is implemented in platform-dependent code, typically written in another programming language such as C.
For example, the class RandomAccessFile of the package java. For a class static method, the monitor associated with the Class object for the method's class is used.
For an instance method, the monitor associated with this the object for which the method was invoked is used. This program defines a class which is designed for concurrent use. Each instance of the class Box has an instance variable boxContents that can hold a reference to any object. You can put an object in a Box by invoking put , which returns false if the box is already full. You can get something out of a Box by invoking get , which returns a null reference if the box is empty.
If put and get were not synchronized , and two threads were executing methods for the same instance of Box at the same time, then the code could misbehave. It might, for example, lose track of an object because two invocations to put occurred at the same time. These type variables are known as the type parameters of the method. A generic method declaration defines a set of methods, one for each possible invocation of the type parameter section by type arguments.
Two methods or constructors M and N have the same type parameters if both of the following are true:. M and N have same number of type parameters possibly zero. Where A 1 , The result of a method declaration either declares the type of value that the method returns the return type , or uses the keyword void to indicate that the method does not return a value. Return types may vary among methods that override each other if the return types are reference types.
The notion of return-type-substitutability supports covariant returns , that is, the specialization of the return type to a subtype. A method declaration d 1 with return type R 1 is return-type-substitutable for another method d 2 with return type R 2 iff any of the following is true:. If R 1 is void then R 2 is void. If R 1 is a primitive type then R 2 is identical to R 1.
If R 1 is a reference type then one of the following is true:. An unchecked conversion is allowed in the definition, despite being unsound, as a special allowance to allow smooth migration from non-generic to generic code.
Essentially, for each checked exception that can result from execution of the body of a method or constructor, a compile-time error occurs unless its exception type or a supertype of its exception type is mentioned in a throws clause in the declaration of the method or constructor. The requirement to declare checked exceptions allows a Java compiler to ensure that code for handling such error conditions has been included.
Methods or constructors that fail to handle exceptional conditions thrown as checked exceptions in their bodies will normally cause compile-time errors if they lack proper exception types in their throws clauses.
The Java programming language thus encourages a programming style where rare and otherwise truly exceptional conditions are documented in this way. A method body is either a block of code that implements the method or simply a semicolon, indicating the lack of an implementation.
More precisely:. It is a compile-time error if a method declaration is either abstract or native and has a block for its body. It is a compile-time error if a method declaration is neither abstract nor native and has a semicolon for its body. In other words, a method with a return type must return only by using a return statement that provides a value return; the method is not allowed to "drop off the end of its body".
It is possible for a method to have a return type and yet contain no return statements. Here is one example:. A class C inherits from its direct superclass type D all concrete methods m both static and instance for which all of the following are true:. No concrete method inherited by C from its direct superclass type has a signature that is a subsignature of the signature of m as a member of D.
A class does not inherit private or static methods from its superinterface types. Note that methods are overridden or hidden on a signature-by-signature basis.
Here, the abstract class Test inherits the abstract method foo from interface I1 and also the abstract method foo from interface I2.
No, because I1 and I2 are not subinterfaces of each other. Thus, from the viewpoint of class Test , the inheritance of foo from I1 is unfettered; similarly for the inheritance of foo from I2.
Note that it is possible for an inherited concrete method to prevent the inheritance of an abstract or default method. An instance method m C declared in or inherited by class C , overrides from C another method m A declared in class A , iff all of the following are true:. C is a subclass of A. C does not inherit m A. One of the following is true:. It is a compile-time error if the overridden method, m A , is a static method. An instance method m C declared in or inherited by class C , overrides from C another method m I declared in interface I , iff all of the following are true:.
I is a superinterface of C. C does not inherit m I. The signature of an overriding method may differ from the overridden one if a formal parameter in one of the methods has a raw type, while the corresponding parameter in the other has a parameterized type. This accommodates migration of pre-existing code to take advantage of generics. The notion of overriding includes methods that override another from some subclass of their declaring class.
This can happen in two ways:. A concrete method in a generic superclass can, under certain parameterizations, have the same signature as an abstract method in that class. In this case, the concrete method is inherited and the abstract method is not as described above. The inherited method should then be considered to override its abstract peer from C. This scenario is complicated by package access: if C is in a different package, then m A would not have been inherited anyway, and should not be considered overridden.
A method inherited from a class can override a superinterface method. Happily, package access is not a concern here. A qualified name or a cast to a superclass type is not effective in attempting to access an overridden method.
In this respect, overriding of methods differs from hiding of fields. The presence or absence of the strictfp modifier has absolutely no effect on the rules for overriding methods and implementing abstract methods. For example, it is permitted for a method that is not FP-strict to override an FP-strict method and it is permitted for an FP-strict method to override a method that is not FP-strict.
Here, the class SlowPoint overrides the declarations of method move of class Point with its own move method, which limits the distance that the point can move on each invocation of the method. When the move method is invoked for an instance of class SlowPoint , the overriding definition in class SlowPoint will always be called, even if the reference to the SlowPoint object is taken from a variable whose type is Point.
Overriding makes it easy for subclasses to extend the behavior of an existing class, as shown in this example:. The class BufferOutput implements a very simple buffered version of an OutputStream , flushing the output when the buffer is full or flush is invoked. The subclass LineBufferOutput declares only a constructor and a single method putchar , which overrides the method putchar of BufferOutput.
It inherits the methods putstr and flush from class BufferOutput. In the putchar method of a LineBufferOutput object, if the character argument is a newline, then it invokes the flush method. The critical point about overriding in this example is that the method putstr , which is declared in class BufferOutput , invokes the putchar method defined by the current object this , which is not necessarily the putchar method declared in class BufferOutput.
Thus, when putstr is invoked in main using the LineBufferOutput object lbo , the invocation of putchar in the body of the putstr method is an invocation of the putchar of the object lbo , the overriding declaration of putchar that checks for a newline. This allows a subclass of BufferOutput to change the behavior of the putstr method without redefining it. Documentation for a class such as BufferOutput , which is designed to be extended, should clearly indicate what is the contract between the class and its subclasses, and should clearly indicate that subclasses may override the putchar method in this way.
The implementor of the BufferOutput class would not, therefore, want to change the implementation of putstr in a future implementation of BufferOutput not to use the method putchar , because this would break the pre-existing contract with subclasses.
If a class C declares or inherits a static method m , then m is said to hide any method m' declared in a class or interface A for which all of the following are true:. A is a superclass or superinterface of C. If A is an interface, m' is an instance method. It is a compile-time error if a static method hides an instance method.
In this respect, hiding of methods is similar to hiding of fields. A class static method that is hidden can be invoked by using a reference whose type is the type of the class that actually contains the declaration of the method. In this respect, hiding of static methods is different from overriding of instance methods. The example:. This rule allows for covariant return types - refining the return type of a method when overriding it. A method that overrides or hides another method, including methods that implement abstract methods defined in interfaces, may not be declared to throw more checked exceptions than the overridden or hidden method.
More precisely, suppose that B is a class or interface, and A is a superclass or superinterface of B , and a method declaration m 2 in B overrides or hides a method declaration m 1 in A.
If m 2 has a throws clause that mentions any checked exception types, then m 1 must have a throws clause, or a compile-time error occurs. It is a compile-time error if a class or interface C has a member method m 1 and there exists a method m 2 declared in C or a superclass or superinterface of C , A , such that all of the following are true:. The declared signature of m 1 or some method m 1 overrides directly or indirectly has the same erasure as the declared signature of m 2 or some method m 2 overrides directly or indirectly.
These restrictions are necessary because generics are implemented via erasure. The rule above implies that methods declared in the same class with the same name must have different erasures. It also implies that a class or interface cannot implement or extend two distinct parameterizations of the same generic interface. The access modifier of an overriding or hiding method must provide at least as much access as the overridden or hidden method, as follows:.
If the overridden or hidden method is public , then the overriding or hiding method must be public ; otherwise, a compile-time error occurs. If the overridden or hidden method is protected , then the overriding or hiding method must be protected or public ; otherwise, a compile-time error occurs. If the overridden or hidden method has package access, then the overriding or hiding method must not be private ; otherwise, a compile-time error occurs.
Note that a private method cannot be overridden or hidden in the technical sense of those terms. This means that a subclass can declare a method with the same signature as a private method in one of its superclasses, and there is no requirement that the return type or throws clause of such a method bear any relationship to those of the private method in the superclass.
The following declarations are legal in the Java programming language from Java SE 5. The relaxed rule for overriding also allows one to relax the conditions on abstract classes implementing interfaces. Now, at some point the author of StringSorter decides to generify the code:.
An unchecked warning would be given when compiling Overrider against the new definition of StringSorter because the return type of Overrider. Incorrect Overriding because of throws. This program uses the usual and conventional form for declaring a new exception type, in its declaration of the class BadPointException :. The program results in a compile-time error, because the override of method move in class CheckedPoint declares that it will throw a checked exception that the move in class Point has not declared.
If this were not considered an error, an invoker of the method move on a reference of type Point could find the contract between it and Point broken if this exception were thrown.
A different compile-time error now occurs, because the body of the method move cannot throw a checked exception, namely BadPointException , that does not appear in the throws clause for move. A class cannot have two member methods with the same name and type erasure:. This is illegal since D. The two methods have the same name, id. The signature of D. Two different methods of a class may not override methods with the same erasure:. This is also illegal, since D. The two methods have different signatures and neither is a subsignature of the other.
It is a compile-time error if a class C inherits a concrete method whose signature is override-equivalent with another method inherited by C. It is a compile-time error if a class C inherits a default method whose signature is override-equivalent with another method inherited by C , unless there exists an abstract method declared in a superclass of C and inherited by C that is override-equivalent with the two methods.
This exception to the strict default- abstract and default-default conflict rules is made when an abstract method is declared in a superclass: the assertion of abstract-ness coming from the superclass hierarchy essentially trumps the default method, making the default method act as if it were abstract. However, the abstract method from a class does not override the default method s , because interfaces are still allowed to refine the signature of the abstract method coming from the class hierarchy.
Note that the exception does not apply if all override-equivalent abstract methods inherited by C were declared in interfaces.
Otherwise, the set of override-equivalent methods consists of at least one abstract method and zero or more default methods; then the class is necessarily an abstract class and is considered to inherit all the methods. One of the inherited methods must be return-type-substitutable for every other inherited method; otherwise, a compile-time error occurs. The throws clauses do not cause errors in this case. There might be several paths by which the same method declaration is inherited from an interface.
This fact causes no difficulty and never, of itself, results in a compile-time error. If two methods of a class whether both declared in the same class, or both inherited by a class, or one declared and one inherited have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded. This fact causes no difficulty and never of itself results in a compile-time error. There is no required relationship between the return types or between the throws clauses of two methods with the same name, unless their signatures are override-equivalent.
Here, the class Point has two members that are methods with the same name, move. Note that Point does not inherit the toString method of class Object because that method is overridden by the declaration of the toString method in class Point. Here, the class RealPoint hides the declarations of the int instance variables x and y of class Point with its own float instance variables x and y , and overrides the method move of class Point with its own move method.
In this example, the members of the class RealPoint include the instance variable color inherited from the class Point , the float instance variables x and y declared in RealPoint , and the two move methods declared in RealPoint.
This following program is an extended variation of the preceding program:. Here, the class Point provides methods getX and getY that return the values of its fields x and y ; the class RealPoint then overrides these methods by declaring methods with the same signature. The result is two errors at compile time, one for each method, because the return types do not match; the methods in class Point return values of type int , but the wanna-be overriding methods in class RealPoint return values of type float.
Here, the overriding methods getX and getY in class RealPoint have the same return types as the methods of class Point that they override, so this code can be successfully compiled. The first line of output illustrates the fact that an instance of RealPoint actually contains the two integer fields declared in class Point ; it is just that their names are hidden from code that occurs within the declaration of class RealPoint and those of any subclasses it might have.
When a reference to an instance of class RealPoint in a variable of type Point is used to access the field x , the integer field x declared in class Point is accessed. The fact that its value is zero indicates that the method invocation p. The second line of output shows that the field access rp. This field is of type float , and this second line of output accordingly displays floating-point values.
Incidentally, this also illustrates the fact that the method name show is overloaded; the types of the arguments in the method invocation dictate which of the two definitions will be invoked. The last two lines of output show that the method invocations p. Indeed, there is no way to invoke the getX method of class Point for an instance of class RealPoint from outside the body of RealPoint , no matter what the type of the variable we may use to hold the reference to the object.
Thus, we see that fields and methods behave differently: hiding is different from overriding. A member interface is an interface whose declaration is directly enclosed in the body of another class or interface declaration. If a class declares a member class or interface with a certain name, then the declaration of the member class or interface is said to hide any and all accessible declarations of member classes and interfaces with the same name in superclasses and superinterfaces of the class.
A class inherits from its direct superclass and direct superinterfaces all the non- private member classes and interfaces of the superclass and superinterfaces that are both accessible to code in the class and not hidden by a declaration in the class.
It is possible for a class to inherit more than one member class or interface with the same name, either from its superclass and superinterfaces or from its superinterfaces alone. However, any attempt within the body of the class to refer to any such member class or interface by its simple name will result in a compile-time error, because the reference is ambiguous. There might be several paths by which the same member class or interface declaration is inherited from an interface.
In such a situation, the member class or interface is considered to be inherited only once, and it may be referred to by its simple name without ambiguity. The rules in this section apply to constructors in all class declarations, including enum declarations and record declarations.
The SimpleTypeName in the ConstructorDeclarator must be the simple name of the class that contains the constructor declaration, or a compile-time error occurs. Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding. If the last formal parameter of a constructor is a variable arity parameter, the constructor is a variable arity constructor.
Otherwise, it is a fixed arity constructor. The rationale for why only this kind of class has an implicitly declared constructor parameter is subtle. The following explanation may be helpful:.
The member class may have been emitted by a compiler which is different than the compiler of the class instance creation expression. Therefore, there must be a standard way for the compiler of the creation expression to pass a reference representing the immediately enclosing instance to the member class's constructor.
Consequently, the Java programming language deems in this section that a non- private inner member class's constructor implicitly declares an initial parameter for the immediately enclosing instance. That compiler can represent the immediately enclosing instance how ever it wishes. This instance must be transmitted from the anonymous class to its superclass, where it will serve as the immediately enclosing instance.
Since the superclass may have been emitted by a compiler which is different than the compiler of the class instance creation expression, it is necessary to transmit the instance in a standard way, by passing it as the first argument to the superclass's constructor.
Note that the anonymous class itself is necessarily emitted by the same compiler as the class instance creation expression, so it would be possible for the compiler to transmit the immediately enclosing instance with respect to the superclass to the anonymous class how ever it wishes, before the anonymous class passes the instance to the superclass's constructor. In a normal class declaration, a constructor declaration with no access modifiers has package access.
Unlike methods, a constructor cannot be abstract , static , final , native , strictfp , or synchronized :. A constructor is not inherited, so there is no need to declare it final. An abstract constructor could never be implemented. A constructor is always invoked with respect to an object, so it makes no sense for a constructor to be static. There is no practical need for a constructor to be synchronized , because it would lock the object under construction, which is normally not made available to other threads until all constructors for the object have completed their work.
The lack of native constructors is an arbitrary language design choice that makes it easy for an implementation of the Java Virtual Machine to verify that superclass constructors are always properly invoked during object creation. These type variables are known as the type parameters of the constructor.
It is possible for a constructor to be generic independently of whether the class the constructor is declared in is itself generic. A generic constructor declaration defines a set of constructors, one for each possible invocation of the type parameter section by type arguments.
The type of a constructor consists of its signature and the exception types given by its throws clause. It is a compile-time error for a constructor to directly or indirectly invoke itself through a series of one or more explicit constructor invocations involving this. If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object , then the constructor body implicitly begins with a superclass constructor invocation " super ; ", an invocation of the constructor of its direct superclass that takes no arguments.
Here, the first constructor of ColoredPoint invokes the second, providing an additional argument; the second constructor of ColoredPoint invokes the constructor of its superclass Point , passing along the coordinates. Explicit constructor invocation statements are divided into two kinds:. Alternate constructor invocations begin with the keyword this possibly prefaced with explicit type arguments. They are used to invoke an alternate constructor of the same class.
Superclass constructor invocations begin with either the keyword super possibly prefaced with explicit type arguments or a Primary expression or an ExpressionName.
They are used to invoke a constructor of the direct superclass. They are further divided:. Unqualified superclass constructor invocations begin with the keyword super possibly prefaced with explicit type arguments. Qualified superclass constructor invocations begin with a Primary expression or an ExpressionName.
This may be necessary when the superclass is an inner class. Let C be the class being instantiated, and let S be the direct superclass of C. If a superclass constructor invocation statement is unqualified, then:. If S is an inner member class, but S is not a member of a class enclosing C , then a compile-time error occurs. Otherwise, let O be the innermost enclosing class of C of which S is a member.
If S is an inner local class, and S does not occur in a static context, let O be the immediately enclosing class or interface declaration of S. C must be an inner class of O , or a compile-time error occurs. If a superclass constructor invocation statement is qualified, then:.
If S is not an inner class, or if the declaration of S occurs in a static context, then a compile-time error occurs. Otherwise, let p be the Primary expression or the ExpressionName immediately preceding ". Evaluation of an alternate constructor invocation statement proceeds by first evaluating the arguments to the constructor, left-to-right, as in an ordinary method invocation; and then invoking the constructor.
Evaluation of a superclass constructor invocation statement proceeds as follows:. Let i be the instance being created. The immediately enclosing instance of i with respect to S if any must be determined:.
If S is not an inner class, or if the declaration of S occurs in a static context, then no immediately enclosing instance of i with respect to S exists. Otherwise, if the superclass constructor invocation is unqualified, then S is necessarily an inner local class or an inner member class. If S is an inner local class, let O be the immediately enclosing class or interface declaration of S. If S is an inner member class, let O be the innermost enclosing class of C of which S is a member.
The immediately enclosing instance of i with respect to S is the n 'th lexically enclosing instance of this. While it may be the case that S is a member of C due to inheritance, the zeroth lexically enclosing instance of this that is, this itself is never used as the immediately enclosing instance of i with respect to S.
Otherwise, if the superclass constructor invocation is qualified, then the Primary expression or the ExpressionName immediately preceding ". If p evaluates to null , a NullPointerException is raised, and the superclass constructor invocation completes abruptly. Otherwise, the result of this evaluation is the immediately enclosing instance of i with respect to S.
After determining the immediately enclosing instance of i with respect to S if any , evaluation of the superclass constructor invocation statement proceeds by evaluating the arguments to the constructor, left-to-right, as in an ordinary method invocation; and then invoking the constructor. Finally, if the superclass constructor invocation statement completes normally, then all instance variable initializers of C and all instance initializers of C are executed.
If an instance initializer or instance variable initializer I textually precedes another instance initializer or instance variable initializer J , then I is executed before J. Execution of instance variable initializers and instance initializers is performed regardless of whether the superclass constructor invocation actually appears as an explicit constructor invocation statement or is provided implicitly.
An alternate constructor invocation does not perform this additional implicit execution. Restrictions on Explicit Constructor Invocation Statements. In the code below, ChildOfInner has no lexically enclosing class or interface declaration, so an instance of ChildOfInner has no enclosing instance.
However, the superclass of ChildOfInner Inner has a lexically enclosing class declaration Outer , and an instance of Inner must have an enclosing instance of Outer. The enclosing instance of Outer is set when an instance of Inner is created. Therefore, when we create an instance of ChildOfInner , which is implicitly an instance of Inner , we must provide the enclosing instance of Outer via a qualified superclass invocation statement in ChildOfInner 's constructor.
The instance of Outer is called the immediately enclosing instance of ChildOfInner with respect to Inner. Perhaps surprisingly, the same instance of Outer may serve as the immediately enclosing instance of ChildOfInner with respect to Inner for multiple instances of ChildOfInner. These instances of ChildOfInner are implicitly linked to the same instance of Outer. The program below achieves this by passing an instance of Outer to the constructor of ChildOfInner , which uses the instance in a qualified superclass constructor invocation statement.
The rules for an explicit constructor invocation statement do not prohibit using formal parameters of the constructor that contains the statement. The effect is that manipulation of instance variables in the common instance of Outer is visible through references to different instances of ChildOfInner , even though such references are not aliases in the conventional sense.
If a class contains no constructor declarations, then a default constructor is implicitly declared. The form of the default constructor for a top level class, member class, or local class is as follows:. The default constructor has no throws clause. If the class being declared is the primordial class Object , then the default constructor has an empty body.
Otherwise, the default constructor simply invokes the superclass constructor with no arguments. It is a compile-time error if a default constructor is implicitly declared but the superclass does not have an accessible constructor that takes no arguments and has no throws clause.
The rule that the default constructor of a class has the same accessibility as the class itself is simple and intuitive. Note, however, that this does not imply that the constructor is accessible whenever the class is accessible. The default constructor for Inner is protected. However, the constructor is protected relative to Inner , while Inner is protected relative to Outer.
0コメント