Search code examples
javadefault-constructor

Java constructor does not look the way it should


I am referencing Y. Daniel Liang's book "Introduction To Java Programming, Comprehensive Version, Ninth Edition" when I ask this question. Every time I see an object created by using a constructor, it goes something like this :

Car engine = new Car(). 

But in Daniel Liang's book, I found the following code (only the first 9 lines written here):

   public class SimpleGeometricObject {
      private String color = "white";
      private boolean filled;
      private java.util.Date dateCreated;

      /** Construct a default geometric object */
      public SimpleGeometricObject() {
         dateCreated = new java.util.Date();
   }

What I don't understand is how come the object "dateCreated" is not created in the normal way, ie.:

SimpleGeometricObject dateCreated = new SimpleGeometrciObject();

I'm confused.


Solution

  • Actually dateCreated is an Object from the Class Date which is in package java.util and is inside the Object you are defining which is SimpleGeoMetricObject

    In other words Java guys wrote this:

      package java.util;
    
     public Class Date{
        //with its own attributes 
       public Date(){
          ...
        //and its own constructor
       }
         ...//and it's own methods
    }
    

    And provided it to developpers so we can use it in our own Class/Objects and by the way if you import the package like this import java.util.Date; in the beginning of your file you would only need to construct Date like follows : Date objectDate = new Date();