• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor

 
abhinas raj
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are Three types of Constructor in java please explain
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 1158
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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?
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 1158
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 10422
    63
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic