Java Image Header

Learn Java 3: Code Structure & Intro to OOP

This is the third official article in the Java coding series. The previous article, Learn Java 2: Variables and Data Types, can be found at the following link: http://nytjournal.org/articles/learn_java_3

Knowing how variables work is useful, but programming real Java applications is much more than simply creating variables. In fact, it is impossible to finish a Java project without understanding how they are structured. Today, we will fix this, by discussing how projects are structured, and taking an in-depth look at this by introducing Object-Oriented Programming (OOP).

In Java, projects contain several key components: the source code, libraries, executable products, documentation, and any media (images, sound, etc) that are used. These are organized in folders: src for source code, lib for libraries, dist for executables, and doc for documentation (media does not have a specific folder that it is housed in). The most important folder is by far the src folder, because that is where the actual code is stored. For this reason, we will limit our study of project structure at the moment, with the exception of src, to knowing these components and their basic uses, but not really diving into the details of what they contain.

For now, the only part of a project that is necessary to our case is the src folder. This folder is typically used to store source code for your application (source code is just the term for any written code). When using the Eclipse IDE, as this tutorial series has been, new projects always come with an src folder upon creation.


In Java, source code files have the extension .java, and are known as “Java files”. Each java file defines one class of the same name (more on classes later), with one class containing a “main method”, which looks like this:


public static void main(String[] args) {

}


This is the starting point of a Java program; without defining this, Java does not know where to start running your program, so your project will not do anything. Traditionally, this method is defined in a class all by itself, known as the “main class”.

Until we learn about classes, our programs will generally be one-class (one file), main method only programs, in order to focus on fundamentals of programming rather than rush into complicated concepts.

Now that we know how to start a Java program, we can create a project that we will use to practice various concepts that we will learn as we go. Since this tutorial was started in Eclipse, I will be using Eclipse for all projects and practices. For an intro to setting up Eclipse, see my first tutorial article. Should you choose to use your own IDE, you may have to adapt these tutorials to your own individual case.


To create this project, create a new Java project (see my first tutorial using the link above to learn how to create a new Java project), name it any name you would like (mine will be called “Practice”), and select ‘Finish’. From this point on, we will use this projects for various small demos of coding concepts, as we explore different aspects of programming.


The links to our previous three articles are:


Tagged in : Computer Science

Arthur Lafrance Image

Arthur Lafrance

Arthur Lafrance is a junior at Homestead High School. Having extensive experience with computer programming, he seeks to educate others about coding and technology. He plans to study computer science and engineering in college. He is the NYTJ Director of Branding.