• Post Reply Bookmark Topic Watch Topic
  • New Topic

another silly getter setter program  RSS feed

 
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys






does it matter what order getters and setters are created in?

is there a conventional way?


 
Sheriff
Posts: 11496
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:does it matter what order getters and setters are created in?

I know we encourage people to ask questions in these forums and we often claim that there are no stupid questions. However, we also ask people to ShowSomeEffort (←click that link and read) before asking questions.  Did you make the effort to find out for yourself what happens if you rearrange getters and setters so they are ordered in different ways? If so, what did you find out and what doubts still remained even after extensive experimentation on your part?

is there a conventional way?

No.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar

yes i did switch them around...and the code still ran perfectly
i just wondered if it mattered which were displayed first thats all
i am a beginner and want to get the basics down good

thanks
 
Junilu Lacar
Sheriff
Posts: 11496
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When it comes to display, there are many different styles but that's really what it boils down to, style.  So, if you're a person who doesn't care about mixing and matching or having some sort of semblance of organization in your code, it really doesn't matter in what order you have your methods in your source.  Others, however, may have to follow coding standards that dictate a certain order, like all public methods grouped together up top, then protected methods grouped together next, then package private methods grouped together next, and finally, all the private methods grouped together towards the end of the source file. This is the most common style you'll probably see besides the "any which way" style. I'm sure there are tools that will rearrange the methods in your code for you automatically so that it fits some kind of style. The Getter/Setter wizard dialog window in Eclipse even gives you a way to choose where and how to place the getters/setters that you generate with it.
 
Junilu Lacar
Sheriff
Posts: 11496
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:yes i did switch them around...and the code still ran perfectly

Then it would be in your best interest as a member of this community to explicitly tell people what you have tried prior to asking questions like this. Saying that it's just "another silly question" gives people the impression that you didn't put any effort into seeing for yourself what would happen and that you just ask these questions hoping that someone will be nice enough to let you get away with not doing your part to find the answers to your own doubts.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Junilu

thanks for the info on design and stuff...

ok sorry for the poor choice of words...i didnt ask about a silly question...i just said another silly program
as it was a very basic program and i would regard it as very simple and silly for an expert such as yourself to be able to answer me.

wont do it again
thanks for your help
 
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another common style is to place constructors at the top of the file, and then all other methods are placed after that in alphabetical order. This makes it easier to find the code for a method when using a simple editor or even an IDE where you do not currently have the hierarchy displayed.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks fred!!!
 
Saloon Keeper
Posts: 3336
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I originally put getters and setters next to each other in pairs. A long time ago I switched to having all the getters together first followed by all the setters. This came about as I implemented more and more classes as immutable which meant that I had getters but no setters.
 
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually don't add print lines in my setters and getters. If setters and getters are doing validations, throwing a custom exception or even an UnsupportedOperationException is my way to go but YMMV
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I do want to see the output in console, I prefer adding loggers for debugging.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:I usually don't add print lines in my setters and getters.
Agree. A setXXX method or a getXXX method shou‍ld do just that, not printing something as well.
If setters and getters are doing validations, throwing a custom exception or even an UnsupportedOperationException is my way to go but YMMV
Agree that failure to validate shou‍ld be signalled with an Exception. I would use an IllegalArgumentException or NullPointerException, however. Not unsupported operation, which is intended for methods not implemented.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!