aspose file tools
The moose likes Beginning Java and the fly likes how should the main() look? (coding styles) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how should the main() look? (coding styles)" Watch "how should the main() look? (coding styles)" New topic

how should the main() look? (coding styles)

Luther Adon
Ranch Hand

Joined: Apr 27, 2002
Posts: 51
I am aware there are as many styles of coding as there are programmers but, I ran into a problem on showing my code to a Java "verteran". He basically said my code looked like nothing he had ever seen before. My code is similar to the books I have read to learn java including the books Just Java, Core Jave, and recently Learn to Program Java.
I try to code in strict UML OOP style.
Anyways my main()'s usually look pretty simple but are not just a call to a constructor, more often they instanciate a frame and show it. Is this be the way it should be or when I get a Java job are people gonna look at my code and think I'm out to lunch?
Sam Smoot
Ranch Hand

Joined: Apr 18, 2002
Posts: 238
Example of yours and the "veteran's" code would be nice...

CNSS/NSA Infosec Professional,<br />Software Engineer
Luther Adon
Ranch Hand

Joined: Apr 27, 2002
Posts: 51
Certainly, and thanx for the swift response. This is what one of my main()s looks like:

Now my friend's looks like this:

Further, in my latest book Learn to Program Java there are many new things I am seeing like loads of static variables in the main(). So while my associate's looks simple it seems wrong to me on OOP guidelines as he seems to be basically making classes for nothing but style.
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
One thing I would always expect to see in a main method is the creation of a "new" instance of the containing class. Without that you are limited to calling only static methods and accessing only static members.
I would also expect to see at least some separation between "View" code (setting up and using Swing, AWT etc.) and "Model" code (business rules, stored data etc.) Just writing a bundle of mixed "view" and "Model" code straight into the main method is only really useful for checking that something does what you expect. Onec you have found that out, put it in a method and/or a class which describes what it is for.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

public static void main(String args[]){ setupJFrame(); JButton jb = new JButton("pressure"); jframe.getContentPane().add(jb); jframe.setVisible(true);}

Your code does seem out of place:
-- your button is declared and instantiated as a local variable to main, while it clearly belongs to JFrame class, where it should be a private member variable
-- the add(jb) call belongs to initComponents() method in JFrame
-- replace jframe.setVisible(true) with pack() and show() in JFrame constructor
-- whatever you do in setupJFrame() should not be exposed to main()
-- your resulting main() method should look like this:

Or, if you have more then a simple GUI program (As Frank pointed out), create a separate class to start the application. Something like this:

[ July 15, 2002: Message edited by: Eugene Kononov ]
Have you tried LearnNowOnline?
subject: how should the main() look? (coding styles)