The course is part of this learning path
In this course, we will learn the concepts of microservice and spring framework with a focus on object-oriented programming.
- Object-oriented programming in Java
- Beginner Java developers
- Java developers interested in learning how to Build and Deploy RESTful Web Services
- Java Developers who want to develop web applications using the Spring framework
- Java Developers who want to develop web applications with microservices
- Java Developers who wish to develop Spring Boot Microservices with Spring Cloud
- Basic Java knowledge
Hello there, my friends. In this video, we will talk about Naming Convention Rules in Java. Java naming convention is a rule to follow on the how to name your identifiers such as class, variable, method, etc. So, it's not forced to follow. It's known as a convention, not a mandatory rule. As you see in the table, the class name should start with an uppercase letter and be a noun. For example, String, Car, System, etc. Method name should start with a lowercase letter and be a verb. For example, start(), stop(), actionPerformed(), etc. Variable name should start with lowercase letters. For example, firstName, year, color, etc. By using standard Java naming conventions, you make your code easier to read for yourself and for other programmers. The readability of the Java program is very important. It means less time is spent understanding what the code does.
The most important concepts of the naming convention are case sensitive and camel casing. Java is a case-sensitive language. For example, 'Hello' with the first letter uppercase and 'hello' with the first letter lowercase have different meanings in Java. Camel casing is a naming convention in which the first letter of the second word and a compound word is capitalized. So, you have a name that is combined with two words. The second word will always start with an uppercase letter. For example, actionPerformed, firstName, etc.
Now let's move on to Eclipse and look at some valid and invalid naming. First I will create a new project. I click on the 'File' menu in the upper left corner and select the New Project option. Let's name our project Naming Conventions. I create my project by clicking the 'Finish' button. Now let's create a class in this project. I right-click on the 'Source' folder. I select the New Class option. Let's set the package name first. Let this package be called naming.conventions. Before specifying the class name, let's look at what names are invalid for the class name. First, I write java in lowercase. As you can see, a warning appeared here. The type name is discouraged. By convention, Java type names usually start with an uppercase letter. So, we could create a class like this, but it's not recommended. Now let's put a number in front of the class name. This time we get the error type name is not valid.
The type name "2java" is not a valid identifier. Class names cannot begin with a digit. Let's put the percentage sign instead of the number. Again invalid. Let's replace the percent with the @ sign. Again invalid. In fact, all special characters except the dollar and underscore are invalid. Let's put a dollar sign. This is valid. Let's put an underscore; this is also valid. So, I think you can now understand what is appropriate and what is not suitable for the class name. So, let's create our class. Let the class name be NamingRules. This class will have a main method and I click the 'Finish' button. First let's look at examples of valid and invalid naming for variables. int java, this is valid. int _java, this is valid. int $java, this is valid. int _$java, this is valid. int __$java, this is valid. int _$ java_$, this is also valid.
Now let's look at the invalid naming. int 1java, this is invalid; it cannot start with a digit. int &java, this is invalid; it cannot contain &. int @java, this is also invalid; it cannot contain @. int java@java, also invalid because it doesn't matter if the special character is at the beginning, middle, or end. int public, this is also invalid because the word public is reserved in Java. The reserved keywords in Java can be found in the table in the variables lecture. But int Public is valid because lowercase public is not the same as uppercase public in Java, since it is a case-sensitive language. int $public is also valid. And int _public is also valid. I hope you now understand which variable names are valid and which are not. Finally, let's look at method naming. void java() with the lowercase is valid. void _java() is also valid, it can start with an underscore. void $java() is valid again, it can start with a dollar sign. void _$java() is valid too, it can start with an underscore, and it can contain a dollar sign. void __$java_$() is also valid, because it can start and end with an underscore or dollar sign, and it can contain an underscore or dollar sign. void Java() is valid again, it can start with an uppercase letter, but this type is not recommended because the method name must start with lowercase according to the naming conventions in Java. And void JAVA() is also valid. It can start with and contain uppercase letters. However, this is also not recommended.
Now let's look at the invalid naming for methods. void 1java() is invalid, it cannot start with a digit. void &java() is invalid again, it cannot start with &. void @ java() is invalid again, it cannot start with @. void public() is invalid again because the public is a reserved keyword, but void Public() is valid because public with a lowercase first letter and public with an uppercase first letter are not the same in Java. And void $public() is also valid and void _public() is also valid again. So, I think by now you should understand the naming conventions in Java. So, let's take a short break here. See you in the next lesson my friend.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.