Code Structure in Java

Before we start to code in java, let’s explore the code structure in java.

Code Structure in java

Source file :

A source code file is a text file (with the .java extension) that holds one class definition. The class represents a piece of program and must go within a pair of curly braces. A very tiny application may have just single class.

A class :

A class has one or more methods, instances. In the class, the method hold the instructions and must be declared inside a class that is within the curly braces of the class.

A Method :

A method is basically a set of statements confined within set of opening and closing curly braces.

Anatomy of a class

When we run a java program, JVM starts running and starts looking for the class provided in the command line. After that it starts looking for a method that looks exactly like:

public static void main (String[] args) {
// your code goes here

Next, in main method everything between the curly braces { } is executed by the JVM. At least one class and one main method (so there must be one main method per application, not one main method per class) is present in every Java application.

Let see a simple java code

Now let’s observe code word by word.

This is a simple Java program.
With file name "".

This is a comment. Like most other programming languages, Java lets us enter a remark into a program’s source file. The complier ignores the content inside the comment. A comment mainly describe or explain the code, so for understanding purpose programmers uses comment. The one shown at the top of the program is called a multiline comment and they may be several line long. This type of comment must begin with /* and end with */.

Anything between these two comment symbols is ignored by the compiler.

We will discuss about different types comment in later sections.

The first line

public class MyFirstCode{

public: The public keyword is an access modifier, which allows the programmer to control the visibility of class members. The members preceded by public can be accessed by code outside the class in which it is declared. (The private keyword is used to prevent a member from being used by code defined outside of its class.)

class: To define a new class, class keyword is used. The entire class definition, including all its members and instances will be between the opening curly brace ({) and the closing curly brace (}) of class.

MyFirstCode: - MyFirstCode is a class name identifier.

Note that here the class name is following CamelCase.

{ : Opening curly braces of the class. This signals the start of class body. All the code that includes methods and statement will occur between the class opening curly brace and its closing curly brace.

Now in the next line:

public static void main(String[] args){

public : The public keyword is an access modifier. Here, the main( ) is declared as public, because when the execution of program starts it must be called by code outside of its class.

static : The keyword static allows main( ) to be called without creating object of the class. This is necessary since main( ) is called by the Java Virtual Machine(JVM) before any object is created.

void : void is a return type. The void is a keyword that tells the compiler that the main method does not return any value.

main : main() is the method called when execution of program begins. Java is case-sensitive. Hence, Main is different from main. Classes that does not contain the main() method are also compiled by the java compiler but they can’t be executed directly. If we type Main instead of main, the compiler will still compile our program. However, JVM will report an error if we try to run this class as JVM will not able to find the main( ) method.

(String [] args): Any information that we need to pass to a method is received by variables specified within the set of parentheses that follow the name of the method. These variables are called parameters. If there are no parameters required for a given method, we still need to include the empty parentheses. There is only one parameter in the main() method.
A parameter named args is declared by String args[ ], is an array of instances of the class String. In this case, args receives any command-line arguments present when the program is executed. This will be demonstrated in later section.

{ : Opening braces of the method. This signals the start of main( )’s body.

In the next line

System.out.println(“Hey !! My First Code. ”);

System.out.println : println( ) is a built-in function that displays the string passed to it on console. System is a predefined class that provides access to the system, and out is the output stream that is connected to the console.

(“Hey!! My First Code. ”) : This is the string we want to print.

; : All statements in Java end with a semicolon. The reason that the other lines in the program do not end in a semicolon is that they are not, technically, statements.

In last line

} }

} : First closing braces(}) defines end of main() and second closing braces(}) defines end of class.

Don’t memorize anything now. This was discussed to get you started. Although all the points will be discuss later in detail.

Do You Know?