default constructor in Java versus C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alf P. Steinbach

    #16
    Re: default constructor in Java versus C++

    * Gary Labowitz:[color=blue]
    > "Alf P. Steinbach" <alfps@start.no > wrote in message
    > news:41985c4d.1 151729750@news. individual.net. ..[color=green]
    > > * Tony Morris:[color=darkred]
    > > >
    > > > > I don't think so, Chris. In Java, any constructor that takes no[/color][/color]
    > parameters[color=green][color=darkred]
    > > > > is called a default constructor.
    > > >
    > > > This is not true.
    > > >[/color][/color][/color]

    I didn't write that, Tony did.

    Learn to quote, Gary.

    [color=blue]
    > 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.[/color]

    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?

    Comment

    • John C. Bollinger

      #17
      Re: default constructor in Java versus C++

      Gary Labowitz wrote:[color=blue]
      > 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.[/color]

      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.Objec t, 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
      jobollin@indian a.edu

      Comment

      • Chris Smith

        #18
        Re: default constructor in Java versus C++

        Gary Labowitz wrote:[color=blue]
        > 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.[/color]

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

        --

        The Easiest Way To Train Anyone... Anywhere.

        Chris Smith - Lead Software Developer/Technical Trainer
        MindIQ Corporation

        Comment

        • hiwa

          #19
          Re: default constructor in Java versus C++

          "KiLVaiDeN" <KiLVaiDeN@CaRa MaiL.CoM> wrote in message news:<419857d6$ 0$15080$626a14c e@news.free.fr> ...[color=blue]
          > "Matt" <jrefactors@hot mail.com> wrote in message
          > news:ba8a039e.0 411141911.cb9a8 ad@posting.goog le.com...[color=green]
          > > 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[/color]
          > parameter only.[color=green]
          > >
          > > 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!![/color]
          >
          > 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[/color]

          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.

          Comment

          Working...