What is CamelCase or CamelStyle?
In Java, it is very important to format code properly. Not only do you need to write a program which works, but you need to also format it properly.
There is a set of rules called the Java Code Conventions. So you can check it out right now and start learning it.
However, in this article we will only be covering one part of the Code Conventions, specifically the rules for writing the names of classes, variables, and methods. So let’s go over them!
Rule No. 1 – Names should be “self-explanatory”
When possible, the names you give to classes, variables and methods should be self-explanatory, so that you (or someone else) can take a look at their names and understand what these classes, variables, or methods do.
For example, which is easier to understand?
1 2 |
int s; int size; |
Of course, the answer is “int size.” As the name implies, this variable is responsible for the size of something.
Rule No. 2 – You always need to use CamelStyle
CamelStyle is a style of writing names which consists of several words. Sometimes this method of writing words is called CamelCase. Let’s understand what CamelStyle is through some examples.
Example No. 1
Let’s say we want to give a name to a variable which is responsible for the size of a box. In this case, we give it the following name:
intboxSize;
As you can see, the name of the variable consists of two words ("Box" and "Size"), both words are written as one word, and the second word is written with a capital letter.
Example No. 2
Let’s say a variable is responsible for the goods which I sell in Germany:
int myGoodsGermany;
And here is another variable, one which is responsible for the goods I sell in Poland:
int myGoodsPoland;
If the names consist of two or more words, the words are written as one word, and every word after the first word is written with a capital letter. It looks like a little like a camel's “humps." That's why it's called CamelStyle.
Rule No. 3 – A capital letter or a lower-case letter?
- Classes need to start with a capital letter
- Variables and methods need to start with a lower-case letter
Example No. 1
1 2 3 4 5 |
class Test { // here you write your code; } |
The name of this class is Test. As you can see, the first letter is capitalized. Why? Because it's a class and all classes start with a capital letter.
Example No. 2
1 2 3 4 5 |
class ValidationTest { // here you write your code; } |
The name of this class is ValidationTest, and its name consists of two words: "Validation" and "Test." Because there is more than one word, the name is written CamelStyle. But the first letter of the first word is capitalized, because classes have to be written in such a manner.
Example No. 3
Take a look at the code below. This cod has the following components: the class "TestOfFigure," the method "defineFigure," and the variable "inputFigure."
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Scanner; public class TestOfFigure { static String defineFigure(int inputFigure){ if (inputFigure < 0) { return "Negative figure"; } else { return "Positive figure"; } } public static void main(String[] args){ System.out.println (defineFigure(5)); } } |
The class TestOfFigure is written in CamelStyle and the first letter of the first word is upper-case. Why? Because it’s a class. Class names start with a capital letter.
The method defineFigure is written in CamelStyle, but the first letter of the first word is lower-case. Why? Because it’s a method. Method names start with a lower-case letter.
The variable inputFigure is written in CamelStyle and the first letter of the first word is lower-case. Why? Because it’s a variable. Variable names start with a lower-case letter.