aspose file tools*
The moose likes Beginning Java and the fly likes Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor" Watch "Constructor" New topic
Author

Constructor

abhinas raj
Ranch Hand

Joined: Jun 02, 2012
Posts: 47
What are Three types of Constructor in java please explain
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Well-written, badly written and total rubbish?

Why do you think there are “three types” in the first place? There are lots of types. The important things about constructors, in my opinion, are:-
  • 1: Every class should have an explicit written constructor, even if it is a private no‑args constructor.
  • 2: Every constructor should initialise every instance field to a sensible value (i.e. establish the class invariants). This can be achieved by constructor chaining.
  • 3: A class should have as few constructors as possible consistent with its intended use and establishing its invariants.
  • Mansukhdeep Thind
    Ranch Hand

    Joined: Jul 27, 2010
    Posts: 1157

    Campbell Ritchie wrote:Well-written, badly written and total rubbish?


    Like much of your "sarcastic facts", this too went over my head. What does well-written/badly written and total-rubbish refer to in this context?


    ~ Mansukh
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Mansukhdeep Thind wrote:
    Campbell Ritchie wrote:Well-written, badly written and total rubbish?


    Like much of your "sarcastic facts", this too went over my head. What does well-written/badly written and total-rubbish refer to in this context?


    He means that there are no standard "three types" of constructor, so we can pick pretty much any 3 descriptions we want and they'll be as valid as any other.

  • no-arg, 1-arg, multiple-arg
  • empty body, comments only in the body, Java code in the body
  • top of the .java file, middle of the .java file, bottom of the .java file
  • protected, package scope, public or private


  • and so on...
    Mansukhdeep Thind
    Ranch Hand

    Joined: Jul 27, 2010
    Posts: 1157

    Jeff Verdegan wrote: He means that there are no standard "three types" of constructor, so we can pick pretty much any 3 descriptions we want and they'll be as valid as any other.

  • no-arg, 1-arg, multiple-arg
  • empty body, comments only in the body, Java code in the body
  • top of the .java file, middle of the .java file, bottom of the .java file
  • protected, package scope, public or private


  • and so on...


    Hahahaha!! Lol!! Now I understood. bottom of file Hahaha!!
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 8223
        
      23

    Jeff Verdegan wrote:He means that there are no standard "three types" of constructor, so we can pick pretty much any 3 descriptions we want

    How about:
  • Good.
  • Bad.
  • I was drunk.

  • @abhinas: Seriously though: from the Java compiler's point of view there are only TWO types of constructor:
  • A default constructor.
  • One you write yourself.
  • If you think there are more, perhaps you could enlighten us (and please quote your source).

    Winston

    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Winston Gutkowski wrote:
    @abhinas: Seriously though: from the Java compiler's point of view there are only TWO types of constructor:
  • A default constructor.
  • One you write yourself.



  • And even that distinction is iffy. Once the byte-code has been generated, there's no way to tell whether the constructor came from your source code or was injected by the compiler. I wouldn't even call it a different type of constructor, but I suppose you could make the argument that it is.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Constructor