GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about default constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about default constructor" Watch "about default constructor" New topic
Author

about default constructor

dx wu
Ranch Hand

Joined: Apr 28, 2002
Posts: 60
Is the default constructor's modifier same to class's modifier?
thanks!


good good study , day day up!
Lahiru Abeydeera
Ranch Hand

Joined: Nov 05, 2004
Posts: 44
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

Joined: Apr 28, 2002
Posts: 60
The Singleton Design Pattern allow the constructor's modifier private
Chi Hang Chiu
Greenhorn

Joined: Sep 25, 2004
Posts: 5
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

Joined: Oct 12, 2000
Posts: 5093
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>(){}


42
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Jul 21, 2003
Posts: 522
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.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
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.


Leandro Melo
SCJP 1.4, SCWCD 1.4
Scott Escue
Ranch Hand

Joined: Jan 20, 2005
Posts: 34
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

Joined: Jul 21, 2003
Posts: 522
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.
 
GeeCON Prague 2014
 
subject: about default constructor