views:

1216

answers:

6

In PHP, we (at least the good programmers) always start general variable names with a lower-case letter, but class variables/objects with an upper-case letter to distinguish them. In the same way we start general file names with a lower case letter, but files containing Classes with an upper case letter.

E.g:

<?php
$number=123;
$string="a string";
$colors_array=array('red','blue','red');
$Cat=New Cat();
?>

Are the conventions the same in java, i.e Objects starting with upper-case but the rest with lower case, or does everything start with lower case as I've read in other places?

+4  A: 

The convention is that class names start with Upper Case letter. Variable names are camelCase. Even if the variable references an object it still starts with lower case.

This page should help.

Vincent Ramdhanie
A: 

The conventions really depend on the individual place you're coding for.

In general, from what I've seen classes are CamelCased (with upper case first), methods start with lower case, and variables I've seen all over the place (some CamelCased, some camelCase with first letter lower (EDIT: as mentioned above, this is the norm), some even with hungarian notation). It depends on your style and the style that the project you're working on has adopted.

EdgarVerona
+19  A: 

You can find the naming in the Java Code Conventions.

A quick summary:

  • For classes, use UpperCaseCamelCase.
  • For class members and local variables use lowerCamelCase
  • For packages, use reverse URI, e.g. org.acme.project.subsystem
  • For constants, use ALL_CAPS.
jamesh
+3  A: 
  • variablesAndMethodsLikeThis
  • ClassesLikeThis
  • CONSTANTS_LIKE_THIS
Patrick McElhaney
+4  A: 

Generally, all variables will start with lower case:

int count = 32;
double conversionFactor = 1.5d;

Some people like to put static constants in all case:

public static final double KILOGRAM_TO_POUND = 2.20462262;

Things get more annoying when you deal with acronyms, and there is no real standard on whether you should use:

HTMLHandler myHtmlHandler;

or

HTMLHandler myHTMLHandler.

Now, either way, note that the class names (Object, String, HTMLHandler) always start with a capital letter, but individual object variables start lowercase.

Richard Campbell
A: 

Some people (who are not me) like to differentiate Method variables from Instance variables by prefixing Instance variables with "this." This also fixes the problem that occurs when assigning a parameter to an Instance variable of the same name:

public ConstructorMethod(MyVar variable) {
    this.varable=variable;
}

But then some people feel you should always use that pattern--but I'm not crazy about it--I think it's overkill if you keep your methods and classes small.

Also, some people use a naming pattern for parameters. This (again) comes in handy when you are assigning from a constructor to an Instance variable:

public ConstructorMethod(MyVar pVariable) {
    varable=pVariable;
}

Usually the pattern is pVariable or _variable. I occasionally use this because I find it more readable than this., but it has the disadvantage of making your Javadocs less readable.

In any case, I don't really like the idea of ALWAYS using any of these patterns, They are great to know but if you really need help differentiating them throughout your code, tell Eclipse to show them in different colors.

Bill K