• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

newbie question re: "design patterns" (?)

 
Alan Christen
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

/****************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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Alan Christen
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49369
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic