Menu

Total Beginners Constructor Question

Steve Amos
2008-03-30
2013-04-09
  • Steve Amos

    Steve Amos - 2008-03-30

    I'm probably missing something obvious here or exposing some kind of fundamental ignorance, but I'm experiencing a minor confusion in the "Eclipse and Java for Total Beginners".

    I'm at approximately lesson 10 and I've been noticing that all the constructors for the regular code match the names of the classes for which they are the constructors.

    For example, Books has a Books constructor (named Books), Person has a Person constructor (named Person) and so on.

    But the JUnit derived test classes are different. PersonTest has a testPerson constructor and BookTest has a testBook constructor, et cetera. I.e., the constructor names (or what I think we are told are the constructors) don't match the class names.

    I'm very new to Java (I'm a fossilized C++ guy).  Don't the constructor names have to match the class names in order for the compiler to know which of the functions is the constructor?  This is different for Java than for C++? For a C++ guy, this is a little discombobulating.  Could somebody explain this to me?

    Sorry for what is probably a very stupid question.

    SCA

     
    • Mark Dexter

      Mark Dexter - 2008-03-30

      Hi Steve. This is actually a very good question. The answer is that there is no constructor in the PersonTest class. By definition, the constuctor has the same name as the class and doesn't name any return type (including void). So testBook() and the other methods are just methods, not the constructor.

      In Java, if you omit the constructor, the no-argument version of the constructor of the superclass (in this case TestCase) is called automatically for you. If the superclass does not have a no-argument constructor (e.g., public TestCase() { do something }), you can't get away with this trick.

      I should have explained this, or put in a constructor. If you add the following constructor to the PersonTest class, it will still work exactly the same way:

      public PersonTest {
        super();
      }

      Hope this clears it up for you. When I revise this tutorial, I will remember to explain this. Thanks for pointing this out. Mark

       
    • Mark Dexter

      Mark Dexter - 2008-03-30

      Hi Steve. I should have added that the Sun Java Tutorials website (http://java.sun.com/docs/books/tutorial/index.html) is a great resource for learning Java. It has a good search feature makes it easy to find stuff. For example, if you search on "constructor", you get a very good overview explanation of constructors. Mark

       
    • Steve Amos

      Steve Amos - 2008-03-31

      Ah. I see.

      I think the part that sent me over the threshold was, in lesson 10, "public void testMyLibrary" is labeled specifically as being a constructor,
      "// test constructor". I started imagining all sorts of monsters in the shadows.

      You know how it is when you don't know what you don't know.

      Many thanks for the clarification. Sounds like it works pretty much just like C++, auto constructor chaining wise.

      All the best.

      SCA

       
      • Mark Dexter

        Mark Dexter - 2008-03-31

        Yes, I can see the confustion. It was meant to mean "this test method is testing the constructor of the class under test". Mark

         

Log in to post a comment.