8.6.7
Default Constructor
CLASSES
8.6.7   Default Constructor
If a class contains no constructor declarations, then a
default constructor
 that
takes no parameters is automatically provided:
If the class being declared is the primordial class
Object
, then the default
constructor has an empty body.
Otherwise, the default constructor takes no parameters and simply invokes the
superclass constructor with no arguments.
A compile time error occurs if a default constructor is provided by the com 
piler but the superclass does not have a constructor that takes no arguments.
If the class is declared
public
, then the default constructor is implicitly given
the access modifier
public
 ( 6.6); otherwise, the default constructor has the
default access implied by no access modifier. Thus, the example:
public class Point {
int x, y;
}
is equivalent to the declaration:
public class Point {
int x, y;
public Point() { super(); }
}
where the default constructor is
public
 because the class
Point
 is
public
.
8.6.8   Preventing Instantiation of a Class
A class can be designed to prevent code outside the class declaration from creat 
ing instances of the class by declaring at least one constructor, to prevent the cre 
ation of an implicit constructor, and declaring all constructors to be
private
. A
public
 class can likewise prevent the creation of instances outside its package by
declaring at least one constructor, to prevent creation of a default constructor with
public
 access, and declaring no constructor that is
public
.
Thus, in the example:
class ClassOnly {
private ClassOnly() { }
static String just = "only the lonely";
}
the class
ClassOnly
 cannot be instantiated, while in the example:
180






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

java hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Inexpensive Web Hosting  Jsp Web Hosting

Cheapest Web Hosting Jsp Hosting Cheap Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved