473,749 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

default constructor in Java versus C++

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
18 3001
> 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
Ann

"Matt" <jr********@hot mail.com> wrote in message
news:ba******** *************** **@posting.goog le.com...
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
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
"Chris Smith" <cd*****@twu.ne t> 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
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

"Matt" <jr********@hot mail.com> wrote in message
news:ba******** *************** **@posting.goog le.com...
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
* 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.htm l#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
> > 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.htm l#16823>

That I do.

--
Tony Morris
http://xdweb.net/~dibblego/
Jul 22 '05 #9
jr********@hotm ail.com (Matt) wrote in message news:<ba******* *************** ***@posting.goo gle.com>...

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2613
by: The Directive | last post by:
This code will not compiled: In main function: Dot temp = new Dot( *(new Point( 5, 5 )) ); In Dot class: //Constructor with default arguments. Dot::Dot( Point& point= *(new Point( 5, 5 )),
10
4874
by: cppaddict | last post by:
Hi, I am writing a program and needs to know one of its object members before it can be initialized. It doesn't really matter for my question (which a C++ question, not a windows question), but the class is used in a windows program and has an HWND member ( a handle to a window), and to make an sense the class must know what the member is. But this puts me in the position of having no default constructor, and I've heard that is...
10
2403
by: Ook | last post by:
I'm having trouble comprehending what exactly "default construction" is. I know how to provide a constructor with initial values, so that if I, for example, in my code do this: MyClass zoot(1,2); It would instantiate MyClass, passing 1 and 2 to my constructor which in turn would do whatever I want it to. Would a default constructor be as follows:
2
6739
by: Andrew Robinson | last post by:
I need to create a shared static field for use within a number of different classes. Which one should I be using or are they all really the same thing? public class Widget { private Widget() {} public static string DataField = string.Empty; } versus
7
9295
by: John Grandy | last post by:
make a call to XML Web Service WebMethod ... returns object myArray with no error ... myArray contains objects of type StringKeyStringValue runtime error occurs on accessing properties of myArray <<< ex.Message "Server was unable to process request. --> There was an error generating the XML document. --> UtilityStorageLibrary.StringKeyStringValue
19
6567
by: Andrew J. Marshall | last post by:
I want to create a class that must receive a parameter when instantiated. In other words, I do not want it to have a "Public Sub New()". 1) Does VB.NET create a default public constructor if I do not declare one? 2) I've read that I can have a "Private Sub New()" which should take care of my needs. Comments? Thanks, Andrew
8
5378
dmjpro
by: dmjpro | last post by:
hey i m new in Generic Class in java. look at this code carefully .... class GenerericClass<T> { private T d1,d2; public GenericClass() {
0
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.