• Post Reply Bookmark Topic Watch Topic
  • New Topic

constructor  RSS feed

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default constructor is not created if the class has any constructors of its own.
what does this sentence mean? would anyone give me an example?
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you create a class without specifying any constructors the compiler will insert the default (no-arg) constructor for you. But if you create a constructor with an argument for a class, you will have to explicitly declare the no-arg constructor, the compiler will not add it for you. If you do not declare a no arg constructor you cannot create instances of a class like <code>Class a = new Class()</code>. This will not compile. You will have to create instances with the constructor you created only.<code>Class a = new Class(args)</code>. The real problem arises when you subclass this class. You will have to provide constructors which explicitly make calls to the superclass' constructor using super(args).
[This message has been edited by Jim Hall (edited December 18, 2001).]
 
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you don't declare any constructor in your class the compiler will automatically add a default one. For instance, if you have the following class:

the compiler will modify it as follows:

Now if your class MyClass is as follows

then the compiler won't add the default constructor MyClass()
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ranch Hand
Posts: 5390
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Valentin Crettaz:
if you don't declare any constructor in your class the compiler will automatically add a default one. For instance, if you have the following class:

the compiler will modify it as follows:

[B]if you create an object of a class when no constr if defined then you won't get any compile time error

[/B]
Now if your class MyClass is as follows

then the compiler won't add the default constructor MyClass()
[B] and if you create an object of a class then you will get compile time error

[/B]

HIH


 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ravish:
your stmts are not entirely correct. I would rephrase
them like this:

if you create an object of a class when no constr if defined then you won't get any compile time error

if you create an object of a class when no constr if defined, using a no argument constr, then you won't get any compile time error.
and if you create an object of a class then you will get compile time error
and if you create an object of a class, using a no argument constr, then you will get compile time error.
Please note that you can still create objects of a class with
the other constructors that are defined in the class provided
you have access to them.
regds.
- satya
ps:
The most important fact abt the orig qstn is that we are
discussing about the NO ARGUMENT CONSTRUCTOR.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!