Anatomy of Java Programs

For a quick example of setting up Eclipse and creating a Java program, check out my last tutorial. That tutorial explains the benefits of using Java as your primary programming language, and writing your first Java program. There are literally thousands of programming languages out there, but Java is a strong versatile language for doing most types of computer programming in.

As mentioned in my last Java tutorial, Eclipse is an IDE used for creating Java programs. When you open Eclipse, you choose a workspace to develop in. Workspaces are just folders  on your computer. Usually, a workspace contains projects that represent a program related library.

You can create  as many workspaces as you want and only place related projects in workspaces together. For instance, you might have one workspace for school assignments, and another workspace for work tasks.

Anatomy of a Java Program
Java applications are created in workspaces that contain projects or programs. Programs are made of packages, and packages contain classes. Classes contain methods that contain the actual Java commands.

At the simplest level, projects (or programs) contain packages and each of those packages contains Java classes. Java classes are *.java files. Each individual *.java file is a Java class.

Java classes are made of methods. Each method represents one action the Java class can do. For example, a Java Calculator class might have multiply() and divide() methods. Java methods use Java commands, like print() and sleep(), that are methods themselves. Java methods also have declarations like int and String.

Remember our HelloWorld example from the last tutorial.

package helloworld;

public class HelloWorld {

   public static void main(String[] args) {
	System.out.print("Hello World!");
   }
}

 

The file HelloWorld.java is saved in the helloworld package in the Eclipse workspace of your choice. The HelloWorld class is defined in the HelloWorld.java file. Notice that the name of the *.java file and the class it defines are named the same. Until you get into more complex Java programming, expect this to always be the case.

Packages are part of the name of a Java class. For example, if a person was named John Abe Smith. You might call him John for short, or you might call him John Abe Smith if you want to make sure people know exactly who you are talking about.

The name of our class defined above is HelloWorld, but if you want to call it by its full name, it is called helloworld.HelloWorld. The package name is part of the fully qualified class name. Most of the time, you can call classes by just their class name and ingore their package name. However, if the Java compiler or Eclipse complains that it doesn’t know which class you are referring to, use the fully qualified class name with the package name to avoid confusion.

If you want to refer to a method in a class, such as the main() method in the HelloWorld class, you can refer to it as helloworld.HelloWorld.main(). It actually is a little more complex than that, but we’ll go over the complexities when I explain scoping.

Summary

I’m glossing a lot over the finer points, and not mentioning all kinds of exceptions, but you have to start somewhere. You will learn a lot of the tricky bits by following coding examples and working your way through problems and projects of your own.

In short:

Workspace -> Projects / Programs -> Packages -> Classes -> Methods -> Commands