You are not logged in. Your edit will be placed in a queue until it is peer reviewed.
We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.
- 15The only problem with this is that there is no validation. Any logic as to what a valid Bottle is should be in the Bottle class. However, using your proposed implementation, I can have a bottle with a negative height and diameter - there's no way to enforce any business rules on the object without validating every time the object is used. By using the second method, I can ensure that if I have a Bottle object, it was, is, and always will be a valid Bottle object according to my contract.Thomas Owens– Thomas Owens ♦2010-12-27 20:35:33 +00:00Commented Dec 27, 2010 at 20:35
- 7That's one area where .NET has a slight edge over properties, since you can add a property accessor with validation logic, with the same syntax as if you were accessing a field. You can also define a property where classes can get the property value, but not set it.JohnL– JohnL2010-12-27 22:04:41 +00:00Commented Dec 27, 2010 at 22:04
- 3@user9521 If you are sure that your code will not cause a fatal error with "bad" values, then go for your method. However, if you need further validation, or the ability to use lazy loading, or other checks when data is read or written, then using explicit getters and setters. Personally I tend to keep my variables private and use getters and setters for consistency. This way all my variables are treated the same, regardless of validation and/or other "advanced" techniques.Jonathan– Jonathan2010-12-28 04:29:28 +00:00Commented Dec 28, 2010 at 4:29
- 2The advantage of using the constructor is that it makes it much easier to make the class immutable. This is essential if you wish to write multi-threaded code.Fortyrunner– Fortyrunner2010-12-29 17:36:43 +00:00Commented Dec 29, 2010 at 17:36
- 8I would make the fields final whenever possible. IMHO I would have preferred fields to be final by default and have a keyword for mutable fields. e.g. varPeter Lawrey– Peter Lawrey2010-12-30 01:08:33 +00:00Commented Dec 30, 2010 at 1:08
| Show 5 more comments
How to Edit
- Correct minor typos or mistakes
- Clarify meaning without changing it
- Add related resources or links
- Always respect the author’s intent
- Don’t use edits to reply to the author
How to Format
- create code fences with backticks ` or tildes ~ ```
like so
``` - add language identifier to highlight code ```python
def function(foo):
print(foo)
``` - put returns between paragraphs
- for linebreak add 2 spaces at end
- _italic_ or **bold**
- indent code by 4 spaces
- backtick escapes
`like _so_` - quote by placing > at start of line
- to make links (use https whenever possible) <https://example.com>[example](https://example.com)<a href="https://example.com">example</a>
How to Tag
A tag is a keyword or label that categorizes your question with other, similar questions. Choose one or more (up to 5) tags that will help answerers to find and interpret your question.
- complete the sentence: my question is about...
- use tags that describe things or concepts that are essential, not incidental to your question
- favor using existing popular tags
- read the descriptions that appear below the tag
If your question is primarily about a topic for which you can't find a tag:
- combine multiple words into single-words with hyphens (e.g. design-patterns), up to a maximum of 35 characters
- creating new tags is a privilege; if you can't yet create a tag you need, then post this question without it, then ask the community to create it for you
lang-java