• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Example of yours and the "veteran's" code would be nice...
 
Luther Adon
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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:

Eugene.
[ July 15, 2002: Message edited by: Eugene Kononov ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic