Naming conventions: Difference between revisions
Content added Content deleted
(New post.) |
|||
Line 798: | Line 798: | ||
Other conventions are also in use. |
Other conventions are also in use. |
||
=={{header|Java}}== |
|||
According to Java's naming conventions, all names should be in mixed case: |
|||
classes and interfaces should be nouns beginning with a capital letter, |
|||
methods should be verbs beginning with a lower case letter, |
|||
variables should begin with a lower case letter. |
|||
Names should be descriptive, and single character names should be avoided except for short lived temporary variables. |
|||
Class variables which are static and final should be all upper case with words separated by underscores. |
|||
It is standard practice for an enum to be named in the same way as a class with its constants in all upper case. |
|||
The following program illustrates these naming conventions. |
|||
<syntaxhighlight lang="java"> |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
public class NamingConventions { |
|||
public static void main(String[] arguments) { |
|||
SolarSystem solarSystem = new SolarSystem(); |
|||
solarSystem.showSunDiameter(); |
|||
System.out.println("The planetary system comprises of:"); |
|||
solarSystem.listPlanets(); |
|||
} |
|||
} |
|||
enum Planet { MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE, PLUTO } |
|||
class SolarSystem { |
|||
public SolarSystem() { |
|||
for ( Planet planet : Planet.values() ) { |
|||
planets.add(planet); |
|||
} |
|||
} |
|||
public void showSunDiameter() { |
|||
System.out.println("The diameter of the sun is approximately " + SOLAR_DIAMETER + " km"); |
|||
} |
|||
public void listPlanets() { |
|||
System.out.println(planets); |
|||
} |
|||
private List<Planet> planets = new ArrayList<Planet>(); |
|||
private static final int SOLAR_DIAMETER = 1_390_000; |
|||
} |
|||
</syntaxhighlight> |
|||
{{ out }} |
|||
<pre> |
|||
The diameter of the sun is approximately 1390000 km |
|||
The planetary system comprises of: |
|||
[MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE, PLUTO] |
|||
</pre> |
|||
=={{header|jq}}== |
=={{header|jq}}== |