By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,383 Members | 2,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,383 IT Pros & Developers. It's quick & easy.

default constructor in Java versus C++

P: n/a
I try to compare the default constructor in Java and C++.

In C++, a default constructor has one of the two meansings
1) a constructor has ZERO parameter

Student()
{ //etc...
}

2) a constructor that all parameters have default values

Student(int age = 10, String name = "Joe")
{ //etc...
}

However, In Java, default constructor means a constructor has ZERO parameter only.

Student()
{ //etc...
}

The following will yield compile errors
Student(int age = 10, String name = "Joe")
{ //etc...
}

Any ideas why Java doesn't support that?

Please advise. Thanks!!
Jul 22 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
> Any ideas why Java doesn't support that?

This might help.
http://www.google.com/search?hl=en&l...zation+C%2B%2B

--
Tony Morris
http://xdweb.net/~dibblego/
Jul 22 '05 #2

P: n/a
Ann

"Matt" <jr********@hotmail.com> wrote in message
news:ba*************************@posting.google.co m...
I try to compare the default constructor in Java and C++.

In C++, a default constructor has one of the two meansings
1) a constructor has ZERO parameter

Student()
{ //etc...
}

2) a constructor that all parameters have default values

Student(int age = 10, String name = "Joe")
{ //etc...
}

However, In Java, default constructor means a constructor has ZERO parameter only.
Student()
{ //etc...
}

The following will yield compile errors
Student(int age = 10, String name = "Joe")
{ //etc...
}

Any ideas why Java doesn't support that?

Please advise. Thanks!!


Same reason algol60 doesn't support it.
Maybe you can tell us why C++ doesn't support all the
algol60 features.
Jul 22 '05 #3

P: n/a
Matt wrote:
I try to compare the default constructor in Java and C++.

Please note that the two languages use that term in different ways.
It's an implementation-level concept, and naturally implementation-level
concerns will differ when the language is changed. In this case, it's
quite by coincidence that the term happens to have a meaning in both
languages, and it results in some confusion when the C++ meaning is
applied to Java.

You seem to be using the C++ definition of "default constructor" below.
The C++ definition does not apply in Java. In Java, all creations or
objects MUST specify an argument list for the constructor, so there is
no "default" in that sense.

A "default constructor" in Java, though, generally means a constructor
that's generated for you by the compiler. For example, the following
class:

class A { }

has a default constructor, generated automatically by the compiler,
which takes no arguments. The following class:

class B
{
public B() { }
}

is identical in functionality, but it does *not* have a default
constructor. Instead, the no-argument constructor is defined quite
explicitly.
However, In Java, default constructor means a constructor has
ZERO parameter only.
It is true that, in Java, all default constructors have zero parameters.
However, it is not true that all zero-parameter constructors are
"default", and hence not true that default "means" zero-parameter.
The following will yield compile errors
Student(int age = 10, String name = "Joe")
{ //etc...
}

Any ideas why Java doesn't support that?


One of the design goals of Java is to simplify the resolution of syntax.
The task of default parameters is easily accomplished by overloading, so
there is no need for the redundant mechanism.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Jul 22 '05 #4

P: n/a
"Chris Smith" <cd*****@twu.net> wrote in message
news:MP************************@news.altopia.net.. .
<<snip>>
A "default constructor" in Java, though, generally means a constructor
that's generated for you by the compiler. For example, the following
class:

class A { }

has a default constructor, generated automatically by the compiler,
which takes no arguments. The following class:

class B
{
public B() { }
}

is identical in functionality, but it does *not* have a default
constructor. Instead, the no-argument constructor is defined quite
explicitly.


I don't think so, Chris. In Java, any constructor that takes no parameters
is called a default constructor.
This could be better checked on comp.lang.java.programmer, however.
--
Gary
Jul 22 '05 #5

P: n/a
I don't think so, Chris. In Java, any constructor that takes no parameters
is called a default constructor.
This is not true.
This could be better checked on comp.lang.java.programmer, however.
--


When did a Usenet forum become the definitive source?
Try Java Language Specification Second Edition 8.6.7 Default Constructor.

Chris did mention however that class A{} is functionally equivalent to class
B{public B(){}}.
This is not true.
I'll leave it up to you to figure out why (hint: 8.6.7 also).

--
Tony Morris
http://xdweb.net/~dibblego/

Jul 22 '05 #6

P: n/a

"Matt" <jr********@hotmail.com> wrote in message
news:ba*************************@posting.google.co m...
I try to compare the default constructor in Java and C++.

In C++, a default constructor has one of the two meansings
1) a constructor has ZERO parameter

Student()
{ //etc...
}

2) a constructor that all parameters have default values

Student(int age = 10, String name = "Joe")
{ //etc...
}

However, In Java, default constructor means a constructor has ZERO parameter only.
Student()
{ //etc...
}

The following will yield compile errors
Student(int age = 10, String name = "Joe")
{ //etc...
}

Any ideas why Java doesn't support that?

Please advise. Thanks!!


it doesn't support that same syntax, but it can be done easily by simply
doing a default constructor ( without any parameter ) and inside declaring
the default values of the variables.

Student() {
age = 10;
name = "Joe";
//etc..
}

I think Java doesn't support the earlier syntax because it makes the syntax
of the constructor different than the one of other functions, and removing
that features, gives a function declaration quasi equal to the one of the
constructors.

K
Jul 22 '05 #7

P: n/a
* Tony Morris:
I don't think so, Chris. In Java, any constructor that takes no parameters
is called a default constructor.


This is not true.
This could be better checked on comp.lang.java.programmer, however.
--


When did a Usenet forum become the definitive source?
Try Java Language Specification Second Edition 8.6.7 Default Constructor.


You mean 8.8.7.

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

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #8

P: n/a
> > Try Java Language Specification Second Edition 8.6.7 Default
Constructor.

You mean 8.8.7.

<url:

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

That I do.

--
Tony Morris
http://xdweb.net/~dibblego/
Jul 22 '05 #9

P: n/a
jr********@hotmail.com (Matt) wrote in message news:<ba*************************@posting.google.c om>...

Java default constructor is one that you don't write. Never.
It doesn't take parameters.

JLS 8.8.7:

If a class contains no constructor declarations, then a default
constructor that takes no parameters is automatically provided.
C++ default constructor is one that you CAN write.
It CAN take parameters.

ISO/ANSI C++ 12.1.5:
A default constructor for a class X is a constructor of class X that
can be called without an argument. If there is no user-declared
constructor for class X, a default constructor is implicitly
declared. An implicitly-declared default constructor is an inline
public member of its class.
Jul 22 '05 #10

P: n/a
Tony Morris wrote:
Try Java Language Specification Second Edition 8.6.7 Default

Constructor.

You mean 8.8.7.

<url:

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

That I do.


Yes, you're right; the default constructor for A would have had package
access instead of public access. Ya learn something new every day. I'm
still searching for a situation where it would matter, though, given
that the class itself is package-access, so it can't be referenced or
subclassed from outside the package.

(That is what you were referring to, right?)

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Jul 22 '05 #11

P: n/a
jr********@hotmail.com (Matt) writes:
Any ideas why Java doesn't support that?


C++ inherits C's /laizzes-faire/ parameter passing, which allows for
arbitrary numbers of method parameters. In Java, a method's parameters
are part of the signature.
Jul 22 '05 #12

P: n/a
(That is what you were referring to, right?)

Yep :)

--
Tony Morris
http://xdweb.net/~dibblego/

Jul 22 '05 #13

P: n/a
> C++ inherits C's /laizzes-faire/ parameter passing, which allows for
arbitrary numbers of method parameters. In Java, a method's parameters are part of the signature.


This is not true. C++ is just as strict as Java about the number of
arguments passed to the function matching the number of parameters in
the function declaration. However, C++ does have a couple of
mechanisms that change this behavior.

1. Default parameter values: This is the C++ feature, which Java
doesn't have, which the original poster was asking about. In C++ you
can declare a function like:

ResourceBundle getBundle(String baseName, Locale locale =
Locale.getDefault())

This is equivalent to declaring two functions in Java

ResourceBundle getBundle(String baseName)
ResourceBundle getBundle(String baseName, Locale locale)

Note, it is not like pre-ANSI C where the compiler simply didn't check
the number and type of function arguments. The C++ compiler will only
allow you to call this with a (String) or (String, Locale) arguments.
I assume Sun left this feature out of Java because it adds complexity
to the language specification because of the need to specify what
happens in situations like:

ResourceBundle getBundle(String baseName);
ResourceBundle getBundle(String baseName, Locale locale =
Locale.getDefault());

getBundle("bundleName");

It's not intuitive which function should be called in this case.
Still, I often find myself missing the default parameter values
feature.

2. The other feature C++ has that Java doesn't (actually Java 1.5 does)
is more a holdover from C, and is not regularly used in C++. This is
varargs, which allows functions to be declared which take an arbitrary
number and type of arguments. I wouldn't say that the existence of
this feature justifies your statement though, since it's never used in
the standard C++ library, and is rarely used in any C++ libraries that
I've seen.

Jul 22 '05 #14

P: n/a
"Alf P. Steinbach" <al***@start.no> wrote in message
news:41*****************@news.individual.net...
* Tony Morris:
I don't think so, Chris. In Java, any constructor that takes no parameters is called a default constructor.


This is not true.

The JLS says: If a class contains no constructor declarations, then a
default constructor that takes no parameters is automatically provided:

which is true. But if a class contains constructor declarations, you may
supply a default constructor that takes no parameters. I believe the use of
the word "default" means a constructor that takes no parameters, whether it
is explicit or implicit.

What I meant by This could be better checked on comp.lang.java.programmer, however.
--


When did a Usenet forum become the definitive source?
Try Java Language Specification Second Edition 8.6.7 Default Constructor.


You mean 8.8.7.

was that the whole discussion is a Java topic and could be better discussed
with the Java ng. And I see that it is crossposted. And, yes, it is 8.8.7.
--
Gary
Jul 22 '05 #15

P: n/a
* Gary Labowitz:
"Alf P. Steinbach" <al***@start.no> wrote in message
news:41*****************@news.individual.net...
* Tony Morris:

> I don't think so, Chris. In Java, any constructor that takes no parameters > is called a default constructor.

This is not true.

I didn't write that, Tony did.

Learn to quote, Gary.

The JLS says: If a class contains no constructor declarations, then a
default constructor that takes no parameters is automatically provided:

which is true. But if a class contains constructor declarations, you may
supply a default constructor that takes no parameters. I believe the use of
the word "default" means a constructor that takes no parameters, whether it
is explicit or implicit.


Nope.

The Java standard's term for an argument-less constructor in general
is "nullary constructor".

A Java default constructor is not the same as a C++ default constructor.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #16

P: n/a
Gary Labowitz wrote:
The JLS says: If a class contains no constructor declarations, then a
default constructor that takes no parameters is automatically provided:

which is true. But if a class contains constructor declarations, you may
supply a default constructor that takes no parameters. I believe the use of
the word "default" means a constructor that takes no parameters, whether it
is explicit or implicit.


Specifically, the first part of JLS(2e) 8.8.7 says:
If a class contains no constructor declarations, then a _default
constructor_ that takes no parameters is automatically provided:

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

A compile-time error occurs if a default constructor is provided by the
compiler but the superclass does not have an accessible constructor that
takes no arguments.

A default constructor has no throws clause.

It follows that is the nullary constructor of the superclass has a
throws clause, then a compile-time error will occur.

--- end quote ---

Emphasis on the first occurrence of "default constructor" is from the
source -- i.e. the quote is a *definition* of the term. A default
constructor is thus defined to be one provided automatically by Java for
some class, which happens when no other constructor is provided. It has
the properties of taking no arguments, throwing no checked exceptions,
and, except for the constructor for java.lang.Object, doing nothing but
invoking the superclass' no-argument constructor. (And note that the
superclass' constructor is not referred to as a "default constructor".)
Note also in the last sentence quoted, the use of the term "nullary
constructor" to refer to a constructor that takes no arguments.

It is true that some Java programmers rather loosely refer to any
nullary constructor as a default constructor, but this usage is
technically inaccurate, and no one who employs it should be shocked or
offended at being corrected. This is especially true in the context of
discussion occurring in some part in a Java-oriented technical forum
such as comp.lang.java.programmer.
John Bollinger
jo******@indiana.edu
Jul 22 '05 #17

P: n/a
Gary Labowitz wrote:
The JLS says: If a class contains no constructor declarations, then a
default constructor that takes no parameters is automatically provided:

which is true. But if a class contains constructor declarations, you may
supply a default constructor that takes no parameters. I believe the use of
the word "default" means a constructor that takes no parameters, whether it
is explicit or implicit.


A couple others have pointed to the language specification's definition
of default constructor, and I don't see any way of reconciling your
interpretation to the document. This is clearly a definition. The word
"default constructor" is italicized in the text, and is contained in a
section entitled "Default Constructor". It contains obviously normative
statements about the default constructor. The text even contains the
phrase "a default constructor that takes no arguments", which would be
somewhat clumsy if the definition of a default constructor were simply
that it takes no arguments.

The JLS uses the phrase "nullary constructor" as others have said, but I
find that relatively few developers are familiar with that term and it
causes confusion, so I prefer to say "no-argument constructor".

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Jul 22 '05 #18

P: n/a
"KiLVaiDeN" <Ki*******@CaRaMaiL.CoM> wrote in message news:<41***********************@news.free.fr>...
"Matt" <jr********@hotmail.com> wrote in message
news:ba*************************@posting.google.co m...
I try to compare the default constructor in Java and C++.

In C++, a default constructor has one of the two meansings
1) a constructor has ZERO parameter

Student()
{ //etc...
}

2) a constructor that all parameters have default values

Student(int age = 10, String name = "Joe")
{ //etc...
}

However, In Java, default constructor means a constructor has ZERO

parameter only.

Student()
{ //etc...
}

The following will yield compile errors
Student(int age = 10, String name = "Joe")
{ //etc...
}

Any ideas why Java doesn't support that?

Please advise. Thanks!!


it doesn't support that same syntax, but it can be done easily by simply
doing a default constructor ( without any parameter ) and inside declaring
the default values of the variables.

Student() {
age = 10;
name = "Joe";
//etc..
}

I think Java doesn't support the earlier syntax because it makes the syntax
of the constructor different than the one of other functions, and removing
that features, gives a function declaration quasi equal to the one of the
constructors.

K


And you can this in Java:

public Student(){ //no-arg constructor
this(10, "Joe", /*etc.*/); //call another constructor
}

public Student(int age){
this(age, "Joe", /*etc.*/); //call another constructor
}

//etc.
Jul 22 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.