Getting Started


In the video lesson, you created a Java class called 'MyLittleProgram' and created a method with the name 'main'.

In this lesson you will understand these terms in detail.

What is a class in Java?

A 'class' is the fundamental building block in Java programming language. Your program, which consists of Java code, is put inside a class.

A simple valid class with the name Tutorial would be:

class Tutorial{


A class, in its simplest form is defined with class keyword and the name of the class. In this case, we called our class ‘Tutorial’ but you can call whatever you want as long as you follow identifier rules. Every class starts with a left curly brace {, and ends with a right curly brace }.

Even though Tutorial class is empty, and there is no code in it, it is still a valid class as you can compile this class without errors. You can create this class using any text editor. When you save the file on the filesystem, the name of the file should exactly match the name of the class. This is especially true for classes with public modifier. So you have to save the file as

To run a java program you have to follow two steps:

  • Compile your java program using javac executable.
  • Run your compiled program using java executable.

Let us understand both the steps in detail.

Compiling a Java Program

To compile this Java program file you would execute the below command from the Terminal:


javac is an executable which is part of JDK.

When you execute the above command a compiled file with the extension of .class is generated.

Note: Note the .java extension. If you miss the .java extension the javac executable will not be able to compile your file to .class file and instead you will see an error displayed with a message saying 'error: Class names, 'Tutorial', are only accepted if annotation processing is explicitly requested'. If you see this message, rerun the command by adding the .java extension.

Running a Java Program

You run the compiled version of your program by using the java executable. To run the compiled class you would execute:

java Tutorial

When the above command is invoked a JVM is spawned and the JVM will run your Java program.

Note: Notice when you run the java executable you will not use the .class extension. However, the java executable picks up the file name with the .class to execute the program.

But the above program will throw an exception when you try to run the program as there is no main method in the class. You will learn more about the main method below.

Java Setup

  • Open Google Cloud Shell using Link: You need a Gmail a/c to get access to your own Cloud Shell
  • Find the java version installed on your computer by using java -version

In an IDE like AndroidStudio, as soon as you save your Java program file, IDE automatically compiles the file using javac and we do not have to do this step.

What is an IDE?

IDE stands for Integrated Development Environment. IDE is a complex software consisting of numerous tools necessary to make software development easy for the Developer. With an IDE you never really compile and run using 'javac' or 'java'. 'javac' is automatically invoked as soon as your file is saved in the IDE and 'java' is invoked when you select a button typically called as 'run' in the IDE interface. Using an IDE improves productivity as it highlights errors in code even before it is saved, can easily search for methods, classes and dependencies and much much more. Popular IDE's for writing Java are Eclipse, IntelliJ, NetBeans etc.. For writing Android programs, the official IDE is AndroidStudio. Android programs can be written in Java and in this course that is what we will use and use AndroidStudio to write your Android apps.

What is an Identifier?

The name of the class is called identifier literal in Java. Identifier literals are also used to name methods, variables etc. All identifier literals are constructed with the same set of rules as follows:

Rules for Identifiers

Cannot start with a number

Can have a mix of letters and numbers and special characters $ and _

Cannot use one of the reserved keywords

Cannot use spaces - E.g; 'my class' is illegal

Identifiers are case-sensitive. They must be written the same way every time you reference them in your program. e.g. MyClass is not the same as myClass

Reserved keywords

abstract continue for new switch
assert default goto package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while

Although it is hard to make a mistake of using the reserved keywords, if you are using an IDE, as most of the IDE's warns us as soon as you try to use it. And the reserved keywords are color coded in an IDE so it is hard to miss it. So it is actually trivial but it is still good to have a glance at the keywords. An IDE makes it easy, fast and less error prone to write Java programs when compared to using simple text editors.

Recommendation for class names

  • Computer does not care -- any name for the class, which satisfies the rules of the Identifier is fine. However you write programs to not only the computer to execute the program but also for other human beings to read and understand your programs! So it is recommended to give meaningful names for a class and also follow Java standard naming conventions:
    A class name should start with an uppercase and should be of a noun form.

  • If you decide to give two words for your class name then use camel casing. E.g., MyLittleProgram. The first letter of every word starts with an uppercase letter.

  • Even though you can use $ and _ for constructing a class name, it is better to avoid using them. Some programs which generates java classes do use $ symbol in the automatically generated class names but human programmers tend to avoid them.

If you remember from the last lesson, you also constructed a main method and added a System.out.println statement inside the main method. So...

What is a main method?

main method is coded as;

public static void main(String[] args) {

In the Java programming language, every standalone application must contain a main method whose signature is:

public static void main(String[] args)

The modifiers public and static can be written in either order (public static or static public), but the general practice is to use public static as shown above. You can name the argument anything you want, but naming it ‘args’ is again a general practice. The main method accepts a single argument of type String array.

The entire line of the method definition, which includes all the modifiers, the method name and its arguments is called the method signature -

public static void main(String[] args)

You will learn more about method signatures, modifiers, String and Arrays in the following chapters.

When a program has a main method, you can run that program from command line. The main method is the entry point for your application and all other methods are invoked later by your program. In our simple example, you only have a System.out.println statement and there were no other methods. So the program started the execution from the main method and executed System.out.println statement which printed out "hello" in the output.

Program Arguments

The single argument that the main method accepts is a way through which you can pass information to your application during runtime. These arguments are also called program arguments. If you had to run the Tutorial program from command line then you would open a terminal (a.k.a. console) and invoke JRE's java executable by keying in:

java Tutorial arg1 arg2 arg3

arg1, arg2, and arg3 are the program arguments (a.k.a. command-line arguments). You can pass zero to many program arguments. arg1/2/3 are just placeholders for sending any values to your program.


A standalone application is made of one or more java files in which at least one file will have the main method. A user can start the application from the class which contains the main method using JRE's java executable program.

Note: Main method is a special type of method. You will learn more on methods in general later. However for the next few lessons we will only focus on using the main method only.

static, public keywords

If you are wondering what the static and public mean?, then for the time being we will ignore finding the answer. Just remember that the main method should have both static and public only then it will work. You will learn more about these keywords in detail in the next module.

What is a statement?

Let us look at the line:

System.out.println("hello " + args[0] + "!");

This line is called a Statement and every statement ends with a semicolon(;). A Java program is made up of many different statements. In the example statement, we use the System class from the core library to print the "hello world!" message to the standard output (a.k.a. console).

"hello world!" is constructed from String value “hello” which is concatenated with program argument received through args[0] and an exclamation mark. Multiple Strings can be concatenated using plus (+) operator.

Remember to add a space after hello to get the above output



  • A statement always ends with a semicolon - ;
  • Observe the indentation applied to the second line. It is a convention to apply indentation to statements within a block of code. A code block is anything within a set of two curly braces {}.
  • The compiler does not care if you indent or not. You apply it so that humans can understand the code better. Some of the advanced IDE's like Android Studio, automatically indent the code for us.

Recap. Points to remember!

A class, main method and a statement. Let us summarize these three elements:

A Class: Class declaration in our example has three components:

  1. Optional modifiers public, the meaning of which you will learn later.

  2. Give any name of the noun form with first letter capitalized and follow java literal naming conventions.

  3. The class body is surrounded by left and right curly braces, {}.

At the minimum a class must have a name and a set of curly braces for the JRE to compile. However to run it from the command line it has to have a main method.

A main method: Main method should have

  1. public and static keywords in any order

  2. Method return type should be void

  3. Method name should be 'main'

  4. Should receive argument of type a String array represented by String[]. You can name the parameter what ever you want although typically 'args' is used.

  5. Mandatory method body, containing optional statements enclosed between left and right curly braces which forms the main method code block.

A Statement: A Statement is a single line of executable. They end with a semicolon ‘;’ System.out.println statement will enable us to write something to the console. That is why we see ‘Hello World’ printed on the console.

Key Terms

What is a Java Program (a.k.a. application)

A typical Desktop Java program is a collection of one or more classes with typically one of them containing the main method, which is the program execution starting point. Java source file can contain more than one class definition however the file name should match the public class name. Each class definition is compiled into separate .class file containing the Java byte code. The name of the .class file matches the class name defined in the .java file. All .java files must be first compiled to .class files and then you run the .class file.

What is Java Runtime Environment (JRE)

The Java Runtime Environment (JRE) provides the libraries, the Java Virtual Machine, and other components to run applications written in the Java programming language. The JRE does not contain tools and utilities such as _javac_ compiler for creating a .class file. However _java_ executable is part of JRE, so using JRE you can run a compiled Java program. JRE is already installed on the Google Cloud Shell so we did not have to install it. If you use an IDE like Android Studio, then JRE is also bundled with it, so you did not have to download that separately.

What is Java Development Kit (JDK)

The JDK is a superset of the JRE, and contains everything that is in the JRE, plus more - tools such as _javac_ executable, and debuggers necessary for developing applications. Using JDK you can convert a .java file into a .class file. Since JDK is already installed on Google Cloud Shell, we did not have to install it. JDK is also bundled with Android Studio. However if you using neither of these environments, you will have to download JDK and install it before you can compile and run your Java programs.

What is JVM?

Java Virtual Machine (JVM) is a program which can run Java programs. When you run your Java program using `java` executable, the JRE spawns a JVM program which inturn runs your specific Java program. Every Java program which is run from the command line will be executed in a separate JVM program.

image alt text

Points to note

  • Every operating system has its own version of JDK and JRE. Popular operating systems (OS) on which you can install JDK and JRE are Windows, Linux, MacOS
  • Once a Java program is compiled on one OS, it can run on any other OS which has JRE installed. This is why Java had a debut with the slogan "Write once run anywhere!"

Basic Linux Commands That Every Developer Should Know

results matching ""

    No results matching ""