class Main {
public static void main(String[] args) {
System.out.println(”Hello world”);
}
}
class User { String name = ‘John’; }
Welcome to the second Java tutorial. In this lesson we will be looking at how our example Hello World Program works.
First we have the class. Lets talk about classes. Lets create another class called User here. I will just type a simple line of code. When i compile this Main.java, Main.class and User.class is created. So when you run javac, or the compiler, what happens is, the classes within that java source file will be compiled to a .class file.
So lets try that again. This time, I will change the name of the Main class to Start. When i compile Main.java, Start.class and User.class is created.
Ok, so I want you to understand that a java source file is just a bunch of classes. And by java source file I mean the files with .java extension: the java file that you made yourself. The code within the java source file is called the source code. And these classes somehow work together to form a program. We will learn how those classes work together later in this tutorial series.
What is important now is that naturally all your java code has to be within a class. In our case it could be the Main class, or the User class. The only exception to this rule would be import statements and package statements, which we will deal with later in this tutorial series.
Now I will return the name of this class from Start to Main, and I will just keep this User class.
Ok, next we have the main function. When you compile this file and create Main.class, and run Main.class using the java command, java will look for a function named main and execute it first. So the main function is a special function that is recognized as the starting point of a program. Thus every java program will have a function named main. For example, if we run User, there will be an error because we do not have the function main within User. Let’s try that out. The error reads public static void main(String[] args) is not defined in the User class. If we define the main function, this will run. You do not have to understand what public static void is for now. You are going to learn about void soon, and learn about public static later on in this tutorial series.
Ok, the next thing we have is System.out.println. This is how you output to the console. We are going to learn about output, in the next tutorial.
All I want you to understand from this lesson is that a Java source file, or source code, consists of a bunch of classes, and compiling a source file creates individual .class files for each class.
In the next lesson, we are going to take a look at the what the System.out.println is.