Java Style Guide
While some programming languages share style characteristics, it’s still important to follow each language’s formatting conventions. We’ll give you an overview of the more common topics, but if you’re looking for something specific, Google compiled a good style guide.
Java naming conventions and best practices
As a rule, Java uses camel case (eg. camelCase
) for most names, including variables and methods. However, there are some variations depending on what you are naming.
Class and Interface names use pascal case (eg. PascalCase
), which is like camel case, except they start with a capital letter. For example, the linked list class is named LinkedList
, not linkedList
.
Constants do not use camel case, and instead use snake case (eg. snake_case
) with all uppercase letters. For example, if you wanted to store the constant pi, you could name it VALUE_OF_PI
.
Java for Programmers
Take this course meant for experienced programmers and learn Java, one of the world's most popular languages.Try it for freeFormatting brackets and parentheses in Java
Brackets ({
and }
) must be used for all method and class declarations, as well as conditionals and loops that contain multiple lines of code.
While you can omit brackets for single line conditionals and loops, it’s best practice to use them for readability. For example,
if (true) {return false;}
is much more readable than
if truereturn false;
Along the same lines, parentheses ((
and )
) should be used for the arguments of the conditional or loop, as seen in the example above.
Indentations and spacing in Java
While the amount of whitespace doesn’t affect the compilation and running of code in Java, there are standards that help with readability.
All indentations should be two spaces, and there should be an indentation each time a new block (eg. loop, method, etc) is opened, as seen in the examples above.
There should be spaces before and after keywords and operators. For example, while
x=3;
is valid syntax, placing a space between each side of the operator keeps the code clear and readable:
x = 3;
Conclusion
Following Java’s style guidelines ensures that your code is readable, maintainable, and aligned with industry standards. By using proper naming conventions, consistent bracket placement, and appropriate spacing, you can write clean and professional Java code.
To learn more about Java, explore our collection of Java articles here.
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Meet the full teamRelated articles
- Article
Java Program Structure
Java programs have a specific structure. Let's take a closer look at the Hello World program — line by line! - Article
Java and the Command Line
A brief overview of running Java through the command line. - Article
Python Syntax Guide for Beginners
Learn Python syntax with this beginner-friendly guide. Understand Python indentation, print statements, variables, comments, user input, and more with examples.
Learn more on Codecademy
- Free course
Java for Programmers
Take this course meant for experienced programmers and learn Java, one of the world's most popular languages.Intermediate1 hour - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly17 hours - Free course
Intro to Java
Get started with Java by learning about the basics of a Java program and variables!Beginner Friendly6 hours