This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

about default constructor

 
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!
 
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
 
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
https://coderanch.com/t/143554/Mock-Exam-Errata/certification/Jtips-Quiz-No-default-constructor
 
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>(){}
 
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
 
Ranch Hand
Posts: 3851
  • 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 ]
 
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.
 
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.
 
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.
 
The knights of nee want a shrubbery. And a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic