aspose file tools*
The moose likes Java in General and the fly likes default constructor????? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "default constructor?????" Watch "default constructor?????" New topic
Author

default constructor?????

Venu Navat
Ranch Hand

Joined: Dec 28, 2004
Posts: 30
suppose a java class don't have a constructor...will compiler creates a default constuctor for that class???


-Venu Navat
Kevin Peterson
Ranch Hand

Joined: Mar 05, 2005
Posts: 37
yes it will but it will be empty and taking no args.

ex. public constructor(){}

-Kp
[ March 07, 2005: Message edited by: Kev Peterson ]
Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171
    
    1
...And it will only do that if you haven't defined any other constructors.


Sun Certified Programmer for the Java 2 Platform
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
May be this post is not directly related to this topic, but when i came across a question in a mock Exam , i understood that :

Java compilerinserts a default constructor when there is no constrcutor in the java code it has the following signature public constructor(){}

But if we write a a java class having the above constructor it's not called as the default constructor.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Kevin Peterson
Ranch Hand

Joined: Mar 05, 2005
Posts: 37
Yes I agree but isn't the default constructor that they give you just a method that takes no args and has no implementaion so giving the example of public constructor () {} would be simular to the default constuctor.

-Kp
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Srinivasa Raghavan:
... Java compilerinserts a default constructor when there is no constrcutor in the java code it has the following signature public constructor(){} ...

From the Java Language Specification...

"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."

Ref: http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#16823


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Kevin Peterson:
Yes I agree but isn't the default constructor that they give you just a method that takes no args and has no implementaion so giving the example of public constructor () {} would be simular to the default constuctor...

With the exception of Object itself, there is implicit implementation. From the Java Language Specification...

"If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor takes no parameters and simply invokes the superclass constructor with no arguments."

Ref: http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#16823

So with respect to "default," my understanding is this: Whether specified by the programmer or provided automatically by the compiler, the no-args constructor is called "default" because in the absense of an explicit call to super, this is the constructor implicitly called by the constructors of any subclasses.
[ March 08, 2005: Message edited by: marc weber ]
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Yes, Marc, that's what we call the "default" constructor. It's not necessarily the one provided by the compiler, but the no-argument one which is automatically called by subclasses (and Class.newInstance()) if they don't call another one specifically.


James Carman, President<br />Carman Consulting, Inc.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Venu

Yes the compiler would put in a no-arg constructor in case the class doesn't have any constructor. If you create a single constructor the compiler would not provide the no-arg constructor. So its a good idea to include a no-arg constructor to be on the safer side. The compiler automatic no-arg constructor access level is the access level of the class.

for example :



the default constructor would be like public test() { }.

Just in case you provide a contructor the compiler automatic constructor would not be provided.

Example




There is also one that is automatically inserted(just in case you don't provide) which would cause the above thing to give an error. Think!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by James Carman:
Yes, Marc, that's what we call the "default" constructor. It's not necessarily the one provided by the compiler, but the no-argument one which is automatically called by subclasses (and Class.newInstance()) if they don't call another one specifically.


As far as I know, that's *not* the terminology Sun promotes - they use the term "default constructor" *only* for the one implicitely generated by the compiler. See http://java.sun.com/developer/TechTips/1998/tt0811.html#tip2 for a good example.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Venu Navat
Ranch Hand

Joined: Dec 28, 2004
Posts: 30
if we explicitly call the super class constructor in the subclass constructor...then there will not be any error...

is that correct Anupam Sinha???

--------------------------------------------------------------------------
class ParentClass
{
public ParentClass(String a) // no no-arg constructor
{}
}

public class test22 extends ParentClass
{
String str;
public test22(String a)
{
--> super(a);
this.str = a;
}

public static void main(String args[])
{
test22 t = new test22 ("String");
}
}
---------------------------------------------------------------------------
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Ok, maybe that's what I have been referring to it as and I've been wrong all along. Sulks up to the bar for a beer
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Yes Venu. I was refering to the call to super() that is automatically inserted.

Can you think of any scenario when a call to super() would not be automatically inserted. (other than when you already have inserted super()).
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Anupam Sinha:
Can you think of any scenario when a call to super() would not be automatically inserted. (other than when you already have inserted super()).


It also doesn't get inserted when you chain a constructure using this(...).

Besides that, the only other exception is the constructor of java.lang.Object, obviously...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Ilja Preuss:
... As far as I know, that's *not* the terminology Sun promotes - they use the term "default constructor" *only* for the one implicitely generated by the compiler. See http://java.sun.com/developer/TechTips/1998/tt0811.html#tip2 for a good example.

Hmmm... Actually, I don't see that this example uses the word "default" as being exclusive to the constructor automatically provided by the compiler.

Since the context here is an illustration of how the compiler generates a constructor, there's a subtle implication that this might be "the" default, but this is never stated.

In fact, the wording here consistently allows room for a programmer-supplied "default." For example, "the generated default constructor" (my italics), seems to imply that there might be a default constructor that's not generated.


[ March 09, 2005: Message edited by: marc weber ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Well, the part of the JLS you quoted above defines the term "default constructor"...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Ilja Preuss:
Well, the part of the JLS you quoted above defines the term "default constructor"...

The wording in that section of the JLS is very similar to the other example cited. It talks about "a default constructor ... [being] automatically provided," but does not actually define "default." Is it default because it's provided by the compiler, or is the compiler simply providing a default that the programmer may have left out?

In other words, we might say that the "default" constructor is the one provided by the compiler in the absense of any other constructors. Or we might say that the "default" constructor is the one called by any subclass constructor in the absense of an explicit call to super. Note that in both cases we're talking about a no-arg constructor; but under the second (broader) interpretation, the default constructor might be provided by either the compiler or the programmer. It appears to me that either interpretation is consistent with the JLS wording, so I'm not sure that we actually have a definition.

(Portions of the JLS I quoted above make general statements about "the default constructor." But note that the section begins by introducing "a" default constructor provided by the compiler; and the remarks that follow seem to delineate that particular constructor. My point is that the language remains too vague to make a determination.)
[ March 09, 2005: Message edited by: marc weber ]
Venu Navat
Ranch Hand

Joined: Dec 28, 2004
Posts: 30
code
____________________________________________________________________________

class ParentClass
{
public ParentClass(String a) // no no-arg constructor
{}
}

public class test22 extends ParentClass
{
String str;
public test22(String a)
{ this.str = a; }
public static void main(String args[])
{
test22 t = new test22("String");
}
}
___________________________________________________________________________

suppose if the parentclass is an interface,then call to super() would not be automatically inserted.....
is it correct scenario, anupam sinha???
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Originally posted by venu navat:
code
____________________________________________________________________________

class ParentClass
{
public ParentClass(String a) // no no-arg constructor
{}
}

public class test22 extends ParentClass
{
String str;
public test22(String a)
{ this.str = a; }
public static void main(String args[])
{
test22 t = new test22("String");
}
}
___________________________________________________________________________

suppose if the parentclass is an interface,then call to super() would not be automatically inserted.....
is it correct scenario, anupam sinha???



No, if you do not explicitly extend a class, the parent class is java.lang.Object.
[ March 10, 2005: Message edited by: James Carman ]
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Originally posted by venu navat:
code
public class ParentClass
{ ... }

public class test22 extends ParentClass
{ .... }

suppose if the parentclass is an interface,then call to super() would not be automatically inserted.....
is it correct scenario, anupam sinha???


Isn't this quite impossible public class ParentClass to be an interface?


--
:alex |.::the_mindstorm::.


blog - InfoQ.com
Venu Navat
Ranch Hand

Joined: Dec 28, 2004
Posts: 30
Ali pope.....i mentioned ....IF parentclass is assumed as an interface and if test22 implements it ,then call to super class constructor would not be automatically inserted......bcoz interface donot have constuctors......

i request anupam sinha to respond to this answer......
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Dear Venu



What do you think about this code? Would a call to the super() be inserted?
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
I wish people would actually try to compile or run some of the code before they post it.


This does not compile. There is an error informing you that you must place and explicit call to super(String) in the test22 constructor because the super class has no default/no-args constructor.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Originally posted by Steven Bell:
I wish people would actually try to compile or run some of the code before they post it.


This does not compile. There is an error informing you that you must place and explicit call to super(String) in the test22 constructor because the super class has no default/no-args constructor.


Hi Steven

So what do you think I posted this code for?
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Originally posted by venu navat:
Ali pope.....i mentioned ....IF parentclass is assumed as an interface and if test22 implements it ,then call to super class constructor would not be automatically inserted......bcoz interface donot have constuctors......

i request anupam sinha to respond to this answer......


The parent class for classes which do not explicitly extend any other classes (they can implement as many interfaces as they want) is java.lang.Object.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Originally posted by Anupam Sinha:

Hi Steven

So what do you think I posted this code for?


Sorry, that was aimed at venu who is asking questions the compiler can answer for him.

You know what, just ignore me I'm having a bad day at work. :roll:
[ March 14, 2005: Message edited by: Steven Bell ]
Venu Navat
Ranch Hand

Joined: Dec 28, 2004
Posts: 30
hi anupam,

yes..i got you...if subclass don't extend from superclass,then super()will not be inserted automatically...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by venu navat:
... yes..i got you...if subclass don't extend from superclass,then super()will not be inserted automatically...

venu, I think you need to examine Anupam's example more closely. What does the error imply?
[ March 14, 2005: Message edited by: marc weber ]
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
There is no such thing in Java, that you can write, that is a class without a superclass. All classes have a superclass, all constructors have a call to super in them.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Originally posted by venu navat:
hi anupam,

yes..i got you...if subclass don't extend from superclass,then super()will not be inserted automatically...


No this is not what i meant.

This will probably give you a better understanding.


Now a call to super should flag an error but it doesn't. Can you think why?

All classes are direct or indirect subclasses Object class. This is true for classes only.

Interfaces do not have any such superinterface like Object.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Originally posted by Steven Bell:
There is no such thing in Java, that you can write, that is a class without a superclass. All classes have a superclass, all constructors have a call to super in them.


No not all constructors have a call to super(). Please refer to posts above.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Originally posted by Anupam Sinha:


No not all constructors have a call to super(). Please refer to posts above.


Yes, they do. If you don't put a call to one of the superclass' constructors in your constructor, the compiler will put in a call to the no-argument (so as to not start another argument over the naming) constructor from the superclass if it exists. If there is no no-argument constructor in your superclass and you don't put in a specific call to one of the other constructors, the code will not compile. As Steven said, every constructor (aside from the no-argument constructor in java.lang.Object) will have to call some constructor in its superclass, via the super() mechanism. Otherwise, how will the superclass be constructed?

Your point about interfaces makes no sense. You can't instantiate an interface! Any class that implements the interface, however, will be a subclass of java.lang.Object (somewhere down the line) and will therefore be able to call its superclass' constructor.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi James

Think about this(). Which statement of mine related to interfaces are you refering?
[ March 15, 2005: Message edited by: Anupam Sinha ]
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Even with this() the superclass constructor is still called, the compiler is simply smart enough not to call it twice. Notice that a call to this() must be the first statement in a constructor because it is the call to super().
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Originally posted by Steven Bell:
Even with this() the superclass constructor is still called, the compiler is simply smart enough not to call it twice. Notice that a call to this() must be the first statement in a constructor because it is the call to super().


A call to this() defers the call to the superclass constructor. Certainly a call to this() is not a call to super(). Had it been so then what is the difference between the two?
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Lets follow the path here.

You create a new Sub("string"). calls this() makes call to the Sub() constructor in which the first line is super() then runs the Sub() constructor, then runs the Sub(String) constructor. As I said the compiler is smart enough not to call super() twice. this() acts as the call to super(), if you remove this() an implicit call to super() is put in it's place.
[ March 15, 2005: Message edited by: Steven Bell ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Steven

Yes as you say a call to super() would be made sooner or later but here we are discussing about whether a call to super() would be inserted or not.

So when you have a call to this() would a call to super() be inserted.

Or

is it correct?




Now answer this : "all constructors have a call to super in them." (True / False).
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
You are right in that a this() means there will not be a super() put in. But you are wrong saying that super() will be called 'sooner or later'. My point was that no matter what, super() is the first thing called before anything in your class runs no matter how many constructors with this(some args or no args) you have. There must be a path in which the first thing executed is the call to super(). Having super() and this() in the same constructor would result in two calls to super() so of course that cannot be.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Steven

By sooner or later I mean "super() is the first thing called before anything in your class runs no matter how many constructors with this(some args or no args) you have".

This is a language interpretation thing.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
The main point here is that you can't instantiate a subclass without first calling one of the superclass' constructors. Even if you use a call to this() and call another constructor's logic, that constructor will have to call a superclass' constructor first. To be specific, the first bit of code that executes when you instantiate any object will be the java.lang.Object class' no-argument constructor logic. Does that make sense?
 
 
subject: default constructor?????