The course is part of this learning path
In this course, we'll learn the object-oriented concept in Java.
- Object-Oriented programming concept
- Object & Class
- Access Modifiers
- Naming Conventions
- Static Keyword
- Nested and Inner Classes
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
- No prior knowledge is required about the Java programming language
- Basic computer knowledge
Hi there, in this video we'll 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, etcetera. But it is not forced to follow. So, 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, example String, Car, System, etcetera. Method name should start with a lowercase letter and be a verb, example start(), stop(), actionPerformed(), etcetera.
Variable name should start with lowercase letters, example firstName, year, color, etcetera. 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 indicates that 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 case-sensitive language. For example, 'Hello' with the first letter uppercase and 'hello' with the first letter lowercase are different meanings in Java. Camel casing is a naming convention in which the first letter of the second word in a compound is capitalized. So, you have a name that is combined with two words.
The second word will start with an uppercase letter always, for example, actionPerformed, firstName etcetera. Now, let's move on to Eclipse and look at some valid and invalid naming. First, i'll create a new project and let's name our project 'NamingConventions'. I create my project by clicking the 'Finish' button. Now, let's create a class in this project. I right click on the 'src' folder, I choose 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 naming is 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 can actually create a class like this, but it is 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 percent sign (%) instead of the number. Again, invalid. Let's replace the % 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. Yes, I think it has been understood what is appropriate and what is not suitable for the class name. Now, 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 also valid. 'int _$java_