/****************DISCLAIMER****************/ I am a self-taught, beginning programmer with Java. So what, you ask? Well, there is a *lot* of the "basics" that I don't have...lots of things that aren't so obvious to me. You've been warned. /****************DISCLAIMER****************/
I have access to code written by contractors (no longer present on site) and there are distinct differences I noticed.
In some instances, a class is written like this:
In other cases, it is the "other way around":
What is the difference between the two? Is one design better than the other?
I welcome your comments. Thank you.
Al.
[ October 31, 2008: Message edited by: Alan Christen ] [ October 31, 2008: Message edited by: Alan Christen ]
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
9
posted
0
There's no difference. The order of methods within a source file has no effect.
Although having said that it might have an effect on readability. Some people like to group related methods together (eg all the methods in a interface that the class implements could be grooped together or maybe all public methods are grouped together). But this is all just a matter of style - it has no effect on the generated byte code. [ October 31, 2008: Message edited by: Joanne Neal ]
Joanne
Alan Christen
Greenhorn
Joined: Sep 18, 2006
Posts: 16
posted
0
Makes sense. As long as the end result is what was expected. There is no difference.
Joanne: what is your personal preference, coding style? I am curious...
I personally put the main method first. That's where the program starts, and that's where I would want to start if i were looking at it for the first time. after that... i don't pay much attention, since i seldom ever read straight through the file. I jump around (and that is what search is for) to where i need to go.
Never ascribe to malice that which can be adequately explained by stupidity.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
4
posted
0
Since I often use Eclipse, I tend to follow what they suggest
Static fields, public (if any) first.
Instance fields
Constructors
Static methods, public first, then private.
Instance methods. I tend to put "interesting" methods first and boilerplate later.
But as you say, it is only a convention; you will see different approaches in different places.
Alan Christen
Greenhorn
Joined: Sep 18, 2006
Posts: 16
posted
0
Thanks to all for your input.
It is big deal for a newbie like myself to have guys like you taking time to answer questions like this.
Thanks again.
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
For everything but the simplest program, I prefer to have the main method in its own class. All the main method does is instanciating other classes, wiring the objects together, and then let them do their work. Perhaps some very fundamental error handling, too.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
9
posted
0
Originally posted by Alan Christen: Makes sense. As long as the end result is what was expected. There is no difference.
Joanne: what is your personal preference, coding style? I am curious...
I'm with Fred. I just stick new methods either immediately above or below the method I'm currently working in and then use the IDE structure pane to find them. I do mostly server side programs so i rarely have a main method.