CLASSES(7)
CLASSES(7)
MEMBER TYPE DECLARATIONS
Member classes or member interfaces are nested classes or interfaces. The name of a member type hides any and all accessible declarations of member types with the same name in superclasses and superinterfaces.
If there exists a declaration of a member type in scope before the declaration of another member type with the same name, then these other member type declaration will shadow the earlier one.
If a member class or interface with simple name C is directly enclosed in another class declaration with fully qualified name N, then N.C is the fully qualified name of the member class or interface.
INSTANCE INITIALISERS
An instance initialiser declared in a class is executed when an instance of the class is created.
A named class’ instance initialiser should not throw any exceptions unless the following conditions hold: a. that exception or its supertype must be explicitly declared in the throws clause of the constructor, b. the class must have at least one explicitly declared constructor.
An anonymous class’ instance initialiser can throw any number of exceptions.
An instance initiliser must complete normally.
There must be no return statement in an instance initialiser.
Try to declare instance variables used in instance initialiser before usage even though these instance variable declarations are in scope.
Instance initialisers can refer to the current object this, to any variable that is in scope and can use the keyword super.
STATIC INITIALISERS
Any static initialisers declared in a class are executed when the class is initialized, and together with any field initialisers for class variables, may be used to initialize the class variables of the class.
Notation for static initialisers is: static Block
Static initialisers and class variable initialisers are executed in textual order. Declaration after usage of class variables is discouraged even though the variables may be in scope.
No return statement must appear inside the block of a static initialiser.
The keywords this, super or any type variable declared outside the initialiser are not allowed and produce compile time errors.
CONSTRUCTOR DECLARATIONS
a constructor is used in the creation of an object that is an instance of a class.
Notation:
ConstructorModifiersopt ConstructorDeclarator Throwsopt ConstructorBody
Where the Constructor Declarator is notated thus:
TypeParametersopt SimpleTypeName (FormalParameterListopt)
The simple name of the constructor declarator must be the simple name of the class in which the constructor is declared, otherwise compile time error. Except for the simple name requirement, the constructor looks like a method declaration without any return type.
Very simple example of a constructor:
class Loader {
int x, y;
Loader(){}
}
Constructors are invoked by:
1. class instance creation expressions.
2. conversions and concatenations caused by the string concatenation operator, +.
3. explicit constructor invocations from other constructors.
Access to constructors is governed by access modifiers.
Constructor declarations are not members. They are not inherited and therefore cannot be overridden or hidden.
The formal parameters and formal type parameters of constructors are identical in structure and behaviour to those of a method.
It is a compile time error to declare two constructors whose signatures are override-equivalent or have the same erasure types in a class.
Constructor Modifiers: the choice of constructor modifiers are either one of public, protected or private, and a single one can never be declared twice nor can more than one of the choice above be chosen. If a normal class has a constructor without an access modifier, then the default is chosen. An enum type has default access modifier of private and cannot ever be declared public or protected.
If an annotation a on a constructor corresponds to an annotation type T and T has a meta-annotation m such that m corresponds to annotation.Target, then m must have an element whose value is annotation.ElementType.CONSTRUCTOR, otherwise a compile time error results.
Unlike methods, a constructor cannot be:
a. abstract, because it would not be implemented
b. final, because it is never inherited.
c. Static, because since a constructor is invoked with respect to an object, it makes no sense to declare it static.
d. Synchronized, because it would lock the object under construction, making it unavailable to other threads until all constructors for the object have finished their work.
e. Native, a design decision in java to ensure proper superclass constructor invocations.
f. Strictfp, a design decision to ensure that unlike in method modifiers, the class determines the fp-strictness of the results of the constructor.
Generic constructors: whether or not the class a constructor is in is generic or not, a constructor can be declared generic. A generic constructor must declare one or more type variables known as the formal type parameters of the constructor which form is identical to the type parameter list of a generic class or interface.
The scope of a constructor’s type parameter is the entire declaration itself, including the type parameter section. Type parameters can therefore appear as part of their own bounds or as bounds of other type parameters in the same section.
On invoking a generic constructor, you need not provide the type parameters explicitly, and when not provided, they are inferred.
Constructor throws: identical to that of a method.
The type of a constructor: consists of its signature and exception types given its throws clause.
Constructor body: the first statement of a constructor body may be the explicit invocation of another constructor of the same class or of the direct superclass.
{ExplicitConstructorInvocationopt BlockStatementsopt}
A constructor using this should not directly or indirectly invoke itself, otherwise a compile time error results.
If the constructor is a constructor for an enum type, it is a compile time error for it to invoke the superclass constructor explicitly.
If a constructor does not begin with a. an explicit constructor invocation, b. and the constructor being declared is not part of the primordial class of Object, then the compiler assumes the constructor begins with “super();”, an invocation of the constructor of its direct superclass that takes no arguments.
Except for possible explicit constructor invocations, the body of a constructor is similar to that of a method.
See examples in §8.8.7 of the jls.
Explicit constructor invocations: notations are of this form
1. alternate constructor invocations, beginning with this are used to invoke an alternate constructor for the class and may be prefaced with explicit type arguments.
nonWildTypeopt this(ArgumentListopt);
2. Superclass constructor invocations are either, a. unqualified, or b. qualified.
a. unqualified superclass constructor invocations, begin with super and may be prefaced with explicit type arguments.
nonWildTypeopt super(ArgumentListopt);
b.
Primary.nonWildTypeopt super(ArgumentListopt);
Where nonWildTypes are notated as
package examples;
public class A {
A(){
System.out.println("I am your super man.");
}
}
public class C extends A {
String color;
C(){
this("white"); //call alternate constructor
}
//the alternate constructor
C(String givencolor){
this.color = givencolor;
}
}
class Loader {
public static void main(String[] args) {
//create an instance without arguments and it'll give
//us white.
C newc = new C();
System.out.println("without arguments default color is
"+newc.color);
//creating one with argument, alternate to customise color
C customc = new C("red");
System.out.println("we've chosen to use the color
"+customc.color);
//not that an implicit call to the super is done each
//time the the subclass constructor is invoked.
}
}
Constructor overloading: constructor overloading similar to method overloading.
Default constructor: if a class contains no default constructor, then one is automatically provided that takes no parameters.
If class is Object, then the constructor an empty body, else the default takes no parameters and simply invokes the superclass constructor with no arguments.
A default constructor has no throws clause.
In an enum type, the default constructor is implicitly private. The default constructor is implicitly given the access modifier that is specified for the class!
Preventing instantiation of a class:
A class can prevent code outside of the class declaration from creating instances of the class by:
1. declaring at least one constructor.
2. preventing the creation of an implicit constructor.
3. declaring all constructors to be private.
A public class can likewise prevent the creation of instances outside the package by:
1. declaring at least one constructor
2. preventing the creation of a default constructor with public access.
3. declaring no constructor that is public.
No comments:
Post a Comment