What is CamelCase or CamelStyle?

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

2

In Java it is very important to format the code properly. Yes, not only you need to write a program which is able to work, but it also needs to be properly formatted.

There is a set of rules called Java Code Convention. So you can check it right now and start to learn it.

But in this article we will cover only one part of the Code Convention, namely the rules of name writing for classes, variables and methods. So let’s remember them!

Rule No.1 – Names should be “self-explanatory”

That is, you should give such names to classes, variables and methods, that you can take a look at them and understand for what these classes, variables or methods are responsible.

For instance, which example is more understandable?

Of course, the answer to this question is “int size”. As the name implies, the variable is responsible for a size of something.

Rule No.2– You need to always use the CamelStyle

CamelStyle is a style of writing names, which consist of several words. Sometimes this CamelStyle Vertex Academy method of word writing is called CamelCase. Let’s understand via examples what the CamelStyle is.

Example No.1

Let’s say we want to give a name to a variable that is responsible for a size of a box. In this case we will give it the following name:

intboxSize;

As you can see, the name of the variable consists of two words – Box and Size – and both words are written in one word, and the second word is written with a capital letter.

Example No.2

Let’s say the variable is responsible for goods which I sell in Germany:

int myGoodsGermany;

And here is another variable, which is responsible for goods I sell in Poland:

int myGoodsPoland;

In the examples above the names consist of two and more words, that’s why they are written in one word, in addition each word is written with a capital letter, just as camel “humps”. For this reason, this style of word writing is called CamelStyle.

Rule No.3 – With capital or low-case letter?

  • Classes should be written with a capital letter
  • Names of variables and methods should start with low-case letter

Example No.1

The class is called Test. As you can see, it’s written with a capital letter. Why? Because this is a class, and all classes are written with a capital letter.

Example No.2

The class is called ValidationTest, its name consists of two words – Validation and Test. For this reason, the name is written in CamelStyle. But the first word is written with a capital letter, because classes should be written only in such a manner.

Example No.3

We suggest you to take a look at the code below. There are following components in this code: class TestOfFigure, method defineFigure, variable inputFigure.

The class TestOfFigure is written in CamelStyle with a capital letter. Why? Because it’s a class. Class names are written with a capital letter.

The method defineFigure is written in CamelStyle, but the first word is written with a low-case letter. Why? Because it’s a method. Method names are written with a low-case letter.

The variable inputFigure is written in CamelStyle and with a low-case letter. Why? Because it’s a variable. Names of variables are written with a low-case letter.

 

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Leave a Reply

Your email address will not be published.

Самоучители--узнать детальнее--