1. Technology

The Structure of Java Program - Hello NetBeans World

By

The Structure of Java Program -  Hello NetBeans World

Typically the first program everyone writes in Java is the very simple "Hello World" program. If you followed the instructions in Getting Started with NetBeans to create the Hello NetBeans World program you would have seen that all it does is print out the string "Hello NetBeans World" to an output window. It's a good start but you can be left wondering what exactly did I just do? Is this the typical structure of Java programs?

Hopefully you'll remember that most of the code for the Hello NeBeans World program was generated automatically by NetBeans. When it created the Main Java file, the structure of the program was built for us:

public class Main {

  public static void main(String[] args) {

  }
}

The first thing to notice is the indentation of the lines and how they match the curly brackets (i.e., '{' and '}'). This gives an important clue to how Java programs work. There is a hierarchy of building blocks that fit together to make a program. The two we see above are called a class and a method. The lines that are highlighted show where the class starts and finishes. Everything between those two curly brackets (known as a block) is part of the class. The other two lines show the method, which has it's own pair of curly brackets, and everything within that block is part of the method.

The important thing to know at this stage is a Java program can be made up or one or more classes and that each class can have one or more methods.

The Hello NeBeans World program is made up of one class called Main and one method called main. You might be forgiven for thinking that they have the same name but as far as Java is concerned, the names Main and main are as different as the names Billy and Susan. It's because Java is case sensitive. It believes there is a big difference between a capital letter and a lower case letter. It's something you'll just have to get used to. When you come to naming your own classes and methods my advice is to pick completely different and distinctive names to avoid the case sensitivity headache.

As for the Hello NeBeans World program, the class could have been called anything we fancied, even Susan:

public class Susan {

  public static void main(String[] args) {

  }

}

But the method has to be called main. As we only have one class and one method in the program and all Java programs must have a method called main it's the only name we can use for our method. If we went on to create other methods in the class we could call them whatever we wanted.  For example, I could create another method called goodbyeNetBeansWorld:

public class Main {

  public static void main(String[] args) {

  }

  public static void goodbyeNetBeansWorld(){
      
  }
}

The reason we must have a method called main is because all Java programs need a starting point of execution. Something has to contain the first bit of Java code to be run and it was decided long ago that it would be in a method called main.

The last observation to be made about the overall structure of the Java program is there is a set way to make a class and method. You probably noticed the main and goodbyeNetBeansWorld methods look remarkably similar. This is because classes and methods are made through the use of declarations.

As the name suggests when you write a declaration it's like shouting to the Java world "This is a class!" or "This is a method!". It's just a combination of Java keywords used to define the class (or method). The one thing Java programs hate more than anything else is ambiguity so class declarations and method declarations follow a set of rules to make sure they can't be mistaken for something else.

 

  1. About.com
  2. Technology
  3. Java
  4. Getting Started
  5. The Structure of Java Program - Hello NetBeans World

©2014 About.com. All rights reserved.