Good Java Style: Part 2

王朝java/jsp·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

Good Java Style: Part 2

By Thornton Rose

IntrodUCtion

This is the conclusion of a two-part series on Java coding style. In Good Java Style: Part 1

, I introduced my case for writing Java code using good habits, eXPlained why we should care about the way our code looks, and illustrated some general elements of good Java style. In this part, I illustrate more elements of good style and bring my case to a conclusion.

Source Files

There are many ways that a Java source file can be organized. Here is one that works well:

File header comment (optional).

Package declaration.

Blank line or other separator.

Import statements.

Blank line or other separator.

Class(es).

Example 1. Bad File Organization.

package org.rotpad;

import java.awt.*;

import javax.swing.event.*;

import org.javacogs.*;

import javax.swing.*;

import java.awt.event.*;

class Foo {

...

}

public class RotPad extends JFrame {

...

}

Example 2. Good File Organization.

package org.rotpad;

// Java classes

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

// JavaCogs classes

import org.javacogs.*;

/**

* RotPad is a simple GUI application for performing rotation ciphers on plain

* text.

*

* @author Thornton Rose

* @version 1.0

*/

public class RotPad extends JFrame {

...

}

//-----------------------------------------------------------------------------

/**

* Foo is ...

*

* @author Thornton Rose

* @version 1.0

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航