Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Default Constructor

 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a default constructor of any class has package access or not? can any one give me brief idea about it..
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Default constructors have two access levels, public and default. Public classes get public access level for their default constructors.
 
Charith Fernando
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

Default Constructor which is implicitly inserted by the compiler if there's no constructor, then it takes the access level of the class.

if the access level of the class is public then the access level of the default constructor will be also public, if the access level of the class is default then the access level of the constructor will be also default... and will continue in that fashion for all other access levels...

NOTE: constructors can be of any access level, even the default constructor which the compiler puts in can be of any access level because if the class is not a top level class then it could have any access level. Only top level classes can have only either public or default
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only top level classes can have only either public or default


One "only" too many. Should be "Top level classes can have only either public or default".
 
Charith Fernando
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
agreed with the correction
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic