Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization 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
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Which class has a default constructor?  RSS feed

 
Greenhorn
Posts: 23
  • 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: 9986
206
  • 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: 5791
150
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: 23
  • 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: 9986
206
  • 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: 5791
150
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: 9986
206
  • 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: 63781
209
  • 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: 63781
209
  • 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: 6634
443
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.
 
Ranch Hand
Posts: 67
Debian Eclipse IDE Java
  • 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
 
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!