Example java source file

Example java source file

Code Conventions for the Java TM Programming Language

The information on this page is for Archive Purposes Only

  1. Introduction
    1.1 Why Have Code Conventions
    1.2 Acknowledgments
  2. File Names
    2.1 File Suffixes
    2.2 Common File Names
  3. File Organization
    3.1 Java Source Files
    3.1.1 Beginning Comments
    3.1.2 Package and Import Statements
    3.1.3 Class and Interface Declarations
  4. Indentation
    4.1 Line Length
    4.2 Wrapping Lines
  5. Comments
    5.1 Implementation Comment Formats
    5.1.1 Block Comments
    5.1.2 Single-Line Comments
    5.1.3 Trailing Comments
    5.1.4 End-Of-Line Comments
    5.2 Documentation Comments
  6. Declarations
    6.1 Number Per Line
    6.2 Initialization
    6.3 Placement
    6.4 Class and Interface Declarations
  7. Statements
    7.1 Simple Statements
    7.2 Compound Statements
    7.3 return Statements
    7.4 if, if-else, if else-if else Statements
    7.5 for Statements
    7.6 while Statements
    7.7 do-while Statements
    7.8 switch Statements
    7.9 try-catch Statements
  8. White Space
    8.1 Blank Lines
    8.2 Blank Spaces
  9. Naming Conventions
  10. Programming Practices
    10.1 Providing Access to Instance and Class Variables
    10.2 Referring to Class Variables and Methods
    10.3 Constants
    10.4 Variable Assignments
    10.5 Miscellaneous Practices
    10.5.1 Parentheses
    10.5.2 Returning Values
    10.5.3 Expressions before `?’ in the Conditional Operator
    10.5.4 Special Comments
  11. Code Examples
    11.1 Java Source File Example

Источник

Читайте также:  If statements python syntax

Managing Source and Class Files

Many implementations of the Java platform rely on hierarchical file systems to manage source and class files, although The Java Language Specification does not require this. The strategy is as follows.

Put the source code for a class, interface, enumeration, or annotation type in a text file whose name is the simple name of the type and whose extension is .java . For example:

//in the Rectangle.java file package graphics; public class Rectangle

Then, put the source file in a directory whose name reflects the name of the package to which the type belongs:

The qualified name of the package member and the path name to the file are parallel, assuming the Microsoft Windows file name separator backslash (for UNIX, use the forward slash).

  • class name – graphics.Rectangle
  • pathname to file – graphics\Rectangle.java

As you should recall, by convention a company uses its reversed Internet domain name for its package names. The Example company, whose Internet domain name is example.com , would precede all its package names with com.example . Each component of the package name corresponds to a subdirectory. So, if the Example company had a com.example.graphics package that contained a Rectangle.java source file, it would be contained in a series of subdirectories like this:

. \com\example\graphics\Rectangle.java

When you compile a source file, the compiler creates a different output file for each type defined in it. The base name of the output file is the name of the type, and its extension is .class . For example, if the source file is like this

//in the Rectangle.java file package com.example.graphics; public class Rectangle < . . . >class Helper

then the compiled files will be located at:

\com\example\graphics\Rectangle.class \com\example\graphics\Helper.class

Like the .java source files, the compiled .class files should be in a series of directories that reflect the package name. However, the path to the .class files does not have to be the same as the path to the .java source files. You can arrange your source and class directories separately, as:

\sources\com\example\graphics\Rectangle.java \classes\com\example\graphics\Rectangle.class

By doing this, you can give the classes directory to other programmers without revealing your sources. You also need to manage source and class files in this manner so that the compiler and the Java Virtual Machine (JVM) can find all the types your program uses.

The full path to the classes directory, \classes , is called the class path, and is set with the CLASSPATH system variable. Both the compiler and the JVM construct the path to your .class files by adding the package name to the class path. For example, if

is your class path, and the package name is

then the compiler and JVM look for .class files in

\classes\com\example\graphics.

A class path may include several paths, separated by a semicolon (Windows) or colon (UNIX). By default, the compiler and the JVM search the current directory and the JAR file containing the Java platform classes so that these directories are automatically in your class path.

Setting the CLASSPATH System Variable

To display the current CLASSPATH variable, use these commands in Windows and UNIX (Bourne shell):

In Windows: C:\> set CLASSPATH In UNIX: % echo $CLASSPATH

To delete the current contents of the CLASSPATH variable, use these commands:

In Windows: C:\> set CLASSPATH= In UNIX: % unset CLASSPATH; export CLASSPATH

To set the CLASSPATH variable, use these commands (for example):

In Windows: C:\> set CLASSPATH=C:\users\george\java\classes In UNIX: % CLASSPATH=/home/george/java/classes; export CLASSPATH

Источник

Example java source file

The information on this page is for Archive Purposes Only

This page is not being actively maintained. Links within the documentation may not work and the information itself may no longer be valid. The last revision to this document was made on April 20, 1999

11 — Code Examples

11.1 Java Source File Example

The following example shows how to format a Java source file containing a single public class. Interfaces are formatted similarly. For more information, see «Class and Interface Declarations» on page 4 and «Documentation Comments» on page 9

 /* * @(#)Blah.java 1.82 99/03/18 * * Copyright (c) 1994-1999 Sun Microsystems, Inc. * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. */ package java.blah; import java.blah.blahdy.BlahBlah; /** * Class description goes here. * * @version 1.82 18 Mar 1999 * @author Firstname Lastname */ public class Blah extends SomeClass < /* A class implementation comment can go here. */ /** classVar1 documentation comment */ public static int classVar1; /** * classVar2 documentation comment that happens to be * more than one line long */ private static Object classVar2; /** instanceVar1 documentation comment */ public Object instanceVar1; /** instanceVar2 documentation comment */ protected int instanceVar2; /** instanceVar3 documentation comment */ private Object[] instanceVar3; /** * . constructor Blah documentation comment. */ public Blah() < // . implementation goes here. >/** * . method doSomething documentation comment. */ public void doSomething() < // . implementation goes here. >/** * . method doSomethingElse documentation comment. * @param someParam description */ public void doSomethingElse(Object someParam) < // . implementation goes here. >> 

Источник

Example java source file

The information on this page is for Archive Purposes Only

3 — File Organization

A file consists of sections that should be separated by blank lines and an optional comment identifying each section.

Files longer than 2000 lines are cumbersome and should be avoided.

For an example of a Java program properly formatted, see «Java Source File Example» on page 19.

3.1 Java Source Files

Each Java source file contains a single public class or interface. When private classes and interfaces are associated with a public class, you can put them in the same source file as the public class. The public class should be the first class or interface in the file.

Java source files have the following ordering:

  • Beginning comments (see «Beginning Comments» on page 4)
  • Package and Import statements
  • Class and interface declarations (see «Class and Interface Declarations» on page 4)

3.1.1 Beginning Comments

All source files should begin with a c-style comment that lists the class name, version information, date, and copyright notice:

/* * Classname * * Version information * * Date * * Copyright notice */ 

3.1.2 Package and Import Statements

The first non-comment line of most Java source files is a package statement. After that, import statements can follow. For example:

package java.awt;import java.awt.peer.CanvasPeer; 

3.1.3 Class and Interface Declarations

The following table describes the parts of a class or interface declaration, in the order that they should appear. See «Java Source File Example» on page 19 for an example that includes comments.

Источник

Оцените статью