“What makes sense is not law, syntax, rules or structure” Aaron Betsky
Before we get into the nitty gritty details of how to program in Java, we need to have a general sense of how a Java program looks like.
So in this section we will give a general view about the syntax of a Java program.
Take a look at the example below:
public class Simple {
public static void main(String[] args){
System.out.println("Hello World");
}
}
If we compile and run this example, we will see the message “Hello World” on the screen.
Note: the file that this example is stored in, should have the same name as the name of the class (in this case Simple).
Classes in Java:
The first line of this example is public class Simple:
In Java we use classes to define how an object of type of that class is about to behave when it’s running. If you open any Java program, you’ll see at least one class is defined in its source code.
In this example we have one class and that is named Simple.
Also in order to declare a class we use two keywords before the name of that class:
public: the keyword public is actually an access specifier and it declares whether or not an object can be created out of that class.
There are other types of access specifiers like protected and private.
class: anytime we want to create a class, we use the keyword class before the name of that class. This will declare the next block of code as the body of that class.
Note: the name of a class usually starts with a capital letter.
`{}`: after the name of a class we used braces `{}` and these declare the border of a class. So basically the opening brace `{` declares the beginning of a class and the closing brace `}` declares the end of that class.
In short a class has this structure:
Access-specifiers class ClassName {//body…}
Main method in Java:
Next we have this line in the example:
public static void main(String[] args){//body…}
This is called method.
Methods are the places where we declare the instructions and tasks of a program.
Depending on the need of a class, it can have many methods.
We can say that methods of a class are like the departments of a company.
In a company each department is in charge of working on a specific task right? For example the work of R&D department is to do research and invent new technologies that can be used to improve the product etc.
Or the task of a marketing department is to focus on finding new markets or trying to advertise the product of the company to as many people as possible.
In a program, the tasks are divided into sub-tasks and each method is in charge of one or more of these tasks.
Now in this example we have a method named main and that is the first place or the entry point when the program is running.
Basically the computer will look for the method named main when it wants to run a program and starts from there to run instructions of that program.
Just like when we declare a class, we need to declare the access specifier of a method as well.
Note: for the main method, we have only one choice and that is public. But for other methods we can use private and protected access specifiers as well.
`static`: In Java we work with objects and objects are created from classes. Basically classes are the template or the map from which objects create from. Also for any object that we create from a class, all the methods and other elements of that class will be copied to a new location in the memory.
This means if we create 10 objects from a class, and if in that class we have one method, then each object will have a separate and independent copy of that method. But when a method is declared static like this one in this example, it means there’s only one method with this name no-matter how many objects we create from that class. Also a method that is declared as static will not be accessible from an object. We can only access the method via the name of the class that the method is defined in (the Simple class in this example).
`void`: methods in a class are callable. That means we can call a method and expect a return value or no-value. For example let's say a method in a class is named `printTime()` and any time we call that method, it will return the current time. But there are methods that do not return a value. These types of methods just run a task and that's it, there's no return value or something.
The main method here is an example of a method that runs a set of instructions but does not return any value.
In such case we declare the keyword void before the name of the method to mention that the method does not return a value.
main: a method needs name, otherwise how can we call a method in a program? In this example the name of the method is main.
Note:
-
Any Java program should have one method named `main` exactly like
the one we’ve declared in this example. -
There are some rules on how to set a name of a method which will
be explained in later sections.
(): sometimes for a method to function correctly, it needs an input. For example let’s say there is a method named sum and this method adds two values together and return the result to the caller. But the method needs two values in order to be able to function correctly. The declaration of the need of these two values for the method happens in the parentheses.
For example:
public int sum(int a, int b){
//body...
}
As you can see in the parentheses of this method we have declare two parameters named a and b with the type integer.
That means when we call this method, we need to put two integer values in it so that it can function correctly.
`{}`: any method has a body. This body is the place where we declare the instructions that is allocated to that method. The body of a method starts from the opening brace `{` and ends at the closing brace `}`;
The println method:
In our example, at the body of the main method, we ran this statement:
System.out.println("Hello World");
The name System here is the name of a class and inside of that class there’s an object named out that is created from a class named PrintStream and inside this class we have a method named println (which stands for print line) and that accepts a message and will send that message to the output stream (in this case the screen).
So the message “Hello World” is sent to the output when we called it inside the body of the main method.
At the end, when the program reaches the closing brace } of the main method, it will terminate the program.
Note: in later sections we will dive dip into each of these materials you see here. So for now just try to remember that in a program there are classes, methods etc. and that’s all you need to know for this section.