Introduction to Java – Class Naming and main method

Introduction to Java – The first Java program

In the first tutorial we saw an introduction to java class and objects and then we learnt about Java packages. In this tutorial, we start our wonderful journey into the world of java by writing our first program. We will write the ubiquitous “Hello World” program.

The java file

Programs in java are written in plain text files. You can edit the file directly in a text editor or use an Integrated Development Environment (IDE) such as eclipse. For an absolute beginner, we recommend using a text editor. As you get more comfortable with the syntax you can evaluate the various IDE’s available. Open up a text editor and start typing the following.

package com.studytrails.java.core;
public class HelloWorld {
	
}

The first line gives the package of the class. Look at the tutorial on Java packages to understand how they work. The second line has three words and a bracket. Everything under the opening and closing bracket is part of the HelloWorld class definition. Anything that belongs to this class (state or operation) resides between this brackets.

Introduction to Java – Elements of class definition

Let’s look at what the three words that define the class mean. The first word is public. What this means is that other classes can freely instantiate or create an object of HelloWorld. The other classes can be in the same or different packages. The word public is known as an access modifier. They specify which other classes or objects can access this class.

The second word class is a keyword (keywords are reserved words in java, we will see about them later). The keyword tells the Java compiler that we are trying to define a class in this file.

The third word is the name of the class. There are certain rules that you need to know to name a class

Introduction to Java – Rules for naming java classes

  • There can only be on public class in a file. You cannot define another public class in this file. However, you can have a non-public class in the same file. We will see more about that in later tutorials.
  • The name of the public class should be exactly same as the name of the file.
  • The class name has to be a java identifier which means it cannot be a reserved keyword. You cannot name a class ‘class’ or ‘public’ since java reserves the use of that word.
  • The class name should begin with an alphabet. It cannot begin with a number or special character.
  • The class name can contain numbers at other position (not the first). However, it cannot contain special characters.
  • The general convention is for the first character to be upper case; use camel case if you have more than one word.

the main method in a java class

Let’s finish writing the class

package com.studytrails.java.core;
public class HelloWorld {
	public static void main(String[] args) {
		
	}
}

The main method is an entry point to the class. When we write a class and run it, the JVM needs to know which method to call first. You can have other methods in the class, but if that class has to be called directly from the command line then you need a ‘main’ method. Note that it is perfectly fine to not have a main method in a class. If you have a class called ClassB and a method in it called methodB then it is possible to instantiate classB and call methodB from another class. It’s just not possible to run classB from console since the JVM does not know how to enter the class.

What is a method signature

The words ‘public’, ‘static’, ‘void’, ‘main’ and the input parameters ‘args’ together form the method signature. One method can be differentiated from other by comparing their method signature.

parts of the main method

The first keyword is public. We saw this keyword earlier too and it is an access modifier that tells us that the method can be accessed from outside the class or package. The second word ‘static’ tells us that the method is not bound to a particular object of that class. The method can be called without creating an object of that class. The main method has to be static since it is the entry point the class and at this point we havent had the chance to create the object of that class yet. ‘main’ tells us that it is the main method. The input argument args gives an array of all the parameters that have been passed from the command line.

Printing Hello World

We are now finally ready to print Hello World to the console

package com.studytrails.java.core;
public class HelloWorld {
	public static void main(String[] args) {
          System.out.println("Hello World");
	}
}

The fourth line adds the logic to print Hello World to the command line.

$ javac HelloWorld.java
$ java com.studytrails.java.core.HelloWorld 
Hello World

So what does the fourth line do? It tells the compiler that there is a class called System which has an object called out (which is of type PrintStream) and that object has a method called println. That method takes in any text that is passed to it and prints it out. In our case it just prints it out to console since that is the default destination for System class.

So that wraps up our first java program. We learnt quite a bit here – we learnt about how to name classes; about the main method; about printing to the console. We also got introduced to the public access modifier and the static keyword. In the next tutorial we will learn about imports

Leave a Comment