• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is this, the demo{...} part?  RSS feed

 
Ij Ofili
Greenhorn
Posts: 5
Java Python TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please always tell us where such code comes from. You have copied it wrongly; it shou‍ld read ConstructorDemo. The code won't compile with the second space in line 10. That is supposed to be the name and body of a class.
 
Ij Ofili
Greenhorn
Posts: 5
Java Python TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

Please always tell us where such code comes from. You have copied it wrongly; it shou‍ld read ConstructorDemo. The code won't compile with the second space in line 10. That is supposed to be the name and body of a class.


My bad, I read it from a post over at  webpage.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is invalid code. There is an error in the answer on StackOverflow.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if you correct that error, it is badly written code, with incorrect formatting of names. It is also not good object‑orientation; those fields shouldn't be accessible from other classes.

A constructor has three purposes:-
  • 1: It is intended to give every field a sensible starting value so the object created is in a consistent state.
  • 2: It is intended to allow creation of instances of this class from other code.
  • 3: It is intended to restrict creation of instances of this class from other code. I mean that writing a constructor is like saying, “You can make an object my way, according to this constructor, or not at all.”
  •  
    Ij Ofili
    Greenhorn
    Posts: 5
    Java Python TypeScript
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Even if you correct that error, it is badly written code, with incorrect formatting of names. It is also not good object‑orientation; those fields shouldn't be accessible from other classes.

    A constructor has three purposes:-
  • 1: It is intended to give every field a sensible starting value so the object created is in a consistent state.
  • 2: It is intended to allow creation of instances of this class from other code.
  • 3: It is intended to restrict creation of instances of this class from other code. I mean that writing a constructor is like saying, “You can make an object my way, according to this constructor, or not at all.”


  • Thanks for the response, you have answered another unasked question of mine- What's the purpose of a constructor!
     
    Ij Ofili
    Greenhorn
    Posts: 5
    Java Python TypeScript
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Greg Charles wrote:I'm not sure what the question here is exactly, but it's not related to the Cattle Drive, so I'll move it over to Beginning Java.

    I was only trying to understand if there was any such occasion where the code snippet would make sense.

    I now see, it doesn't. Thanks for all your feedback.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!