Java Syntax

In this tutorial, will walk on syntax of java with examples, understanding of statement terminator, block statements and declaration of method, class, and comments.

Java Syntax

All the java code files or file which are written for compilation should have a file extension of ".java" and name of file should match with the class name of that specific file otherwise the compilation will fail.

Every file which runs in java will have an extension of ".class" and name of the file will match with the class name what is declared.

In java we have in-built keywords, keyword is a word which is reserved for specific purpose or identifier of the code or a part of that code.

Statement terminator syntax

Every statement must be terminated in the Java, after end of every statement should be terminated.

Statement terminator used in java is a symbol of semicolon (;)

System.out.println("Hello Java World");

Above is an example of one statement, which prints a string "Hello Java World" and statement is terminated by a character ;

Block statement syntax

A block statement which contains one or more sequence of statements. A statement or statements which are declared inside the curly braces ({}) are block statements, block can be declared without any statements but does not have any meaning unless there is an annotation.

System.out.println("Hello Java World");

Variables which are declared inside of a block statement, will not be accessible outside of the block. This concept is popularly referred as variable scope.

Class declaration syntax

In java, we have a reserved keyword for class declaration which is "class" and can be declared as following.

class Main {

In the above example can be interpreted as class which is a keyword, Main which is a name of the class and curly braces ({}) is body of the class.

Class can be declared with accessing scope also, as following.

public class Main {

In the above example, public is declared as a scope of the class. Which means class will be accessible or visible globally.

class is a keyword used for declaring the class and class body with in the curly braces.

Class can contain data members, zero or more methods and other class also.

public class Main {
//data members

Method Declaration syntax

Method can be declared starts with its return type, name of the method, parentheses, and body of the method.

void methodName() {

Above example contains return type as void, which means method returns nothing.

And parentheses with empty parameters, which means method is not taking any parameters for processing.

Body of the method enclosed with curly braces ({}) and has no statements to process.

int addTwoNumbers(int x,int y) {
return x+y;

Above example contains return type as int, which means method returns an integer.

And parentheses with two parameters, which means method is taking two parameters for processing.

Body of the method enclosed with curly braces ({}) and contains one statement. This statement does an addition of two integers named as x and y and returns the value of two integers.


In this tutorial, we have learned about the java syntax which includes statement terminator, declaration of block, class, and method.