This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Which class has a default constructor?  RSS feed

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are Java classes:

Class X{}

Class Y {Y () {}}

Class Z{z( int i) {} }

Which class has a default constructor?
 
Saloon Keeper
Posts: 9710
192
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think, and why?

What does the word 'default' mean? What could it refer to in this case?
 
Sheriff
Posts: 5688
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Since this is a Java question, I moved it to the Beginning Java forum.)

Can you answer this question: What is a default constructor?  Then, which classes do you think have one?
 
Anduena Smith
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A default constructor is a no-argument constructor automatically generated . I think only X has it but I'm not sure.
 
Stephan van Hulst
Saloon Keeper
Posts: 9710
192
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct.

'Default' means what will be used when nothing has been specified. Y and Z both declare a constructor, so by definition they don't have a default constructor.
 
Knute Snortum
Sheriff
Posts: 5688
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class Z declares a constructor?  Are you sure?
 
Stephan van Hulst
Saloon Keeper
Posts: 9710
192
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whelp, I totally fell for it. Unless it's a typo of course, and 'z' is supposed to be written 'Z'. Otherwise the class won't compile in the first place.
 
Marshal
Posts: 62881
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The definition of a default constructor is to be found in the Java® Language Specification (=JLS).
 
Campbell Ritchie
Marshal
Posts: 62881
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:. . . 'z' is supposed to be written 'Z'. Otherwise the class won't compile in the first place.

That was a good catch of Knute's, but you are right that Z won't have a default constructor because the default constructor is only added as the class is compiled.
 
Marshal
Posts: 6510
441
BSD Linux Mac OS X VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Non of the source files compile, because class's declaration must start with lower c: class not Class. And end of story with such kind of question.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.


More info at Oracle Website here
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!