Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about default constructor

 
dx wu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the default constructor's modifier same to class's modifier?
thanks!
 
Lahiru Abeydeera
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. It doesn't make sense it to be more public than the class.
on the other hand it can't be less public because it will affect instantiation of a class.

Eg. if a class is public and the default constructor is made private how can anyone outside that class creates an object of the class?


Lahiru - SCJP 1.4
 
dx wu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Singleton Design Pattern allow the constructor's modifier private
 
Chi Hang Chiu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the default constructor must be "public" without arguement.

pls take a reference at
http://www.coderanch.com/t/143554/Mock-Exam-Errata/certification/Jtips-Quiz-No-default-constructor
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Singleton Design Pattern allow the constructor's modifier private


True, but that's not a default constructor.

I think the default constructor must be "public" without arguement


Correct. The default no-argument constructor is public <ClassName>(){}
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, there are two things,
1)Default constructor provided implicitly
2)No-arg constructor which we define explicitly

The 1) Default Constructor, is always public

But when we explicitly define a no-arg constructor i.e 2) , it takes the access as specified, i mean if its
public constr() {
}
// Here its public

constr() {
}
// Here its taken as default
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Jeroen ,
I can't expect such answer from you ...
Default constructor is the one which compiler provide when you don't define any constructor and its access specifier is same as class access specifier .

class Foo {

}

for compiler it is :

class Foo {
Foo() {
super();
}

have you got the point .

[ I accepted the mistake ]
[ January 21, 2005: Message edited by: rathi ji ]
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From JLS 8.8.7

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.
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi rathi ji.

Default constructor is the one which compiler provide when you don't define any constructor and its access specifier is default .


You think you got it wrong.
The default constructor doesn't to have necessaritly "default" access.
Naturally, it'd happen if you don't specify a construct for one of your default class in your file.
However, if you build a public class and don't specify any constructor, your class will still have a constructor generated by jvm, which is also called a default constructor (and has public access). This is the default constructor.

Well, this is my opinion.
 
Scott Escue
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want to throw my two cents in because several people have made good points but I'm not sure anyone has accurately summed everything up.


- The "default" constructor is the no-arg constructor provided by the JVM if you don't provide one. (Just to be clear, a no arg constructor that you code is NOT a default constructor)

- The "default" (JVM provided) constructor takes the access modifier of the class. So if the class is public, the default constructor is public; if the class has default access the default constructor will have default access.

- Since a class cannot be declared with private or protected access, the default constructor cannot have private or protected access. However if you provide the constructor, it can have any access modifier you wish to give it. (This includes private acces which, as was pointed out earlier, is commonly used in singleton classes)
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Scott Escue:
- Since a class cannot be declared with private or protected access, the default constructor cannot have private or protected access...

Your statement is correct for top-level classes. However it is perfectly legal for inner nested classes to have private and protected access modifier.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic