image
Naming Conventions in Java
Start course
Difficulty
Beginner
Duration
2h 5m
Students
188
Ratings
4.6/5
Description

In this course, we'll learn the object-oriented concept in Java.

Learning Objectives

  • Object-Oriented programming concept
  • Object & Class
  • Access Modifiers
  • Naming Conventions 
  • Constructors
  • Packages
  • Static Keyword
  • Nested and Inner Classes

Intended Audience

  • 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

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

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_ , 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 incorrect, it cannot contain ampersand. 'int @java', this is invalid, it cannot contain the @ sign. 'int java@java', this is 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 'public' is reserved in Java. You can look at the reserved keywords in Java from the table in the Variables Lecture.

But, 'int Public' is valid because the 'Public' is not the same as 'public' in Java because Java is the case-sensitive language. And 'int $public' is also valid, and 'int _public' is also valid. I think it's clear which naming is valid or invalid for variables as well. Finally, let's look at the methods. 'void java' with a 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. '_$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 and contain an uppercase letter, but 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 ampersand. 'void @java' is invalid again, it cannot start with an @ sign. 'void public' is invalid again because the public is a reserved keyword. But 'void Public' is valid because 'public' with the first letter is lowercase and 'Public' with the first letter is uppercase are not the same in Java. And 'void $public' is also valid, and 'void _public' is also valid again. Yes, I think you understood the naming conventions in Java. Let's take a short break here, see you in the next lesson.

 

About the Author
Students
4005
Courses
64
Learning Paths
5

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.

Covered Topics