• Post Reply Bookmark Topic Watch Topic
  • New Topic

Default constructor  RSS feed

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone give about the access modifier for the default compiler generated constructors.
If the class is a outer class what is the access modifier applied?
If the class is a inner class what is the access modifier applied?
If the class is top level nested class then what is the access modifier?
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think...the compiler generated constructors have same access as that of the class...correct me if i am wront
 
S Thiyanesh
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if:

Now what could be the access modifier for the default construtors for A & B.
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According me constructor A has default access and constructor B has private
access. Please correct me if i am wrong.
 
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constructor having a private access can be useless since it cannot be accessed by any other class. Its another way to avoid creating an object of that class.

As a standard, we should not put any access modifier for construtors. Thus, for every constructors java automatically assigning default access modifier.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Atul that's not correct. Look at common impls of the Singleton pattern...
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A constructor having a private access can be useless since it cannot be accessed by any other class.


A counterexample:


[ July 17, 2006: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from the Java Language Specification.

8.8.9 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 compiler but the superclass does not have an accessible constructor that takes no arguments.

A default constructor has no throws clause.

It follows that if the nullary constructor of the superclass has a throws clause, then a compile-time error will occur.
In an enum type (�8.9), the default constructor is implicitly private. Otherwise, if the class is declared public, then the default constructor is implicitly given the access modifier public (�6.6); if the class is declared protected, then the default constructor is implicitly given the access modifier protected (�6.6); if the class is declared private, then the default constructor is implicitly given the access modifier private (�6.6); otherwise, the default constructor has the default access implied by no access modifier.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class Modifiers are modifiers for Default constructors.
 
Your mind is under my control .... your will is now mine .... read this tiny ad
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!