This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Default constructor

 
S Thiyanesh
Ranch Hand
Posts: 142
  • 0
  • 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?
 
pramodh somashekara
Ranch Hand
Posts: 62
  • 0
  • 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
  • 0
  • 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.
 
Gowher Naik
Ranch Hand
Posts: 643
  • 0
  • 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.
 
Atul Sawant
Ranch Hand
Posts: 304
  • 0
  • 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.
 
Andy Morris
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Atul that's not correct. Look at common impls of the Singleton pattern...
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • 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 ]
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • 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.
 
Chandrasekhar Mangipudi
Ranch Hand
Posts: 118
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class Modifiers are modifiers for Default constructors.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic