Win a copy of Spark in Action this week in the Open Source Projects 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

Confusion on Constructor

 
Greenhorn
Posts: 7
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was reading a book that says when there is a return type in a function, it is no longer a constructor, rather a method



so this is a valid constructor:



but on the quiz part it was said that "Which of these compile and is a default constructor" so this is one of them:



now im confused whether that void thingy is a constructor   , thanks for reply.
 
Ranch Hand
Posts: 99
15
Chrome Java Windows
  • Likes 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

void Random() { }


This is not a constructor. This is a method. It has return type (void in this case).
A constructor has no return type and has the same name as a class it defined in. Methods can have this name too, but they must provide a return type.
A default constructor (for the public class Random) really looks like this:

but you can't see it in your code. Because a default constructor is a no-parameter constructor which will be created by compiler if you don't provide a constructor in your code.
If you define a constructor by yourself, no default constructor will be created.

What book do you use and how does the quiz question look like?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

gj sadvib wrote:i was reading a book that says when there is a return type in a function, it is no longer a constructor, rather a method


That book is spot-on! And it's always nice to mention some info about the book in the topic title, so it's easier for others (e.g. the authors) to know which resource you are using.

gj sadvib wrote:now im confused whether that void thingy is a constructor  


Always a method, never a constructor because the declaration defines a return type. (Note: if you have posted which book you are reading, it might be listed on the errata overview for this book)
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Viktor Logwin wrote:Because a default constructor is a no-parameter constructor which will be created by compiler if you don't provide a constructor in your code.
If you define a constructor by yourself, no default constructor will be created.


Spot-on! Have a cow!

The default constructor is always a no-arg constructor and will be automatically added by the compiler if you didn't provide any constructors for your class. If you provide your own constructor(s) in a class, the default constructor will not be added by the compiler. You can add a no-arg constructor yourself (if needed), but this is not the default constructor.
 
Die Fledermaus does not fear such a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic