This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes newbie question re: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "newbie question re: "design patterns" (?)" Watch "newbie question re: "design patterns" (?)" New topic
Author

newbie question re: "design patterns" (?)

Alan Christen
Greenhorn

Joined: Sep 18, 2006
Posts: 16
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

Joined: Aug 05, 2005
Posts: 3606
    
  15
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
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

Joined: Oct 02, 2003
Posts: 11357
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39103
    
  23
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
    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
    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: 3606
        
      15
    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.
     
    GeeCON Prague 2014
     
    subject: newbie question re: "design patterns" (?)