Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Coding conventions

100% developed
From Wikibooks, open books for an open world
<Java Programming

KeywordsJava Programming
Coding conventions
Classes and Objects
NavigateLanguage Fundamentals topic:()


The Java code conventions are defined by Oracle in thecoding conventions document. In short, these conventions ask the user to usecamel case when defining classes, methods, or variables. Classes start with a capital letter and should be nouns, likeCalendarDialogView. For methods, the names should be verbs in imperative form, likegetBrakeSystemType, and should start with a lowercase letter.

It is important to get used to and follow coding conventions, so that code written by multiple programmers will appear the same. Projects may re-define the standard code conventions to better fit their needs. Examples include a list of allowed abbreviations, as these can often make the code difficult to understand for other designers. Documentation should always accompany code.

One example from the coding conventions is how to define a constant. Constants should be written with capital letters in Java, where the words are separated by an underscore ('_') character. In the Java coding conventions, a constant is astatic final field in a class.

The reason for this diversion is that Java discerns between "simple" and "complex" types. These will be handled in detail in the following sections. An example for a simple type is thebyte type. An example for a complex type is a class. A subset of the complex types are classes that cannot be modified after creation, like aString, which is a concatenation of characters.

For instance, consider the following "constants":

publicclassMotorVehicle{/** Number of motors */privatestaticfinalintMOTORS=1;/** Name of a motor */privatestaticfinalStringMOTOR_NAME="Mercedes V8";/** The motor object */privatestaticfinalMotorTHE_MOTOR=newMercedesMotor();/**   * Constructor   */publicMotorVehicle(){MOTORS=2;// Gives a syntax error as MOTORS has already been assigned a value.THE_MOTOR=newToshibaMotor();// Gives a syntax error as THE_MOTOR has already been assigned a value.MOTOR_NAME.toLowercase();// Does not give a syntax error, because it returns a new String rather than editing the MOTOR_NAME variable.THE_MOTOR.fillFuel(20.5);// Does not give a syntax error, as it changes a variable in the motor object, not the variable itself.}}


KeywordsJava Programming
Coding conventions
Classes and Objects
Retrieved from "https://en.wikibooks.org/w/index.php?title=Java_Programming/Coding_conventions&oldid=4114558"
Category:
Hidden category:

[8]ページ先頭

©2009-2025 Movatter.jp