The Anatomy of a Program
In this chapter, will be very Java specific, however, some parts, such as a class, method, scope, and code blocks, are common to most high-level programming language. You can put these parts together a develop a comprehensive program. Remember, ORDER MATTERS! But do not worry, I have detailed videos on how to create a Java program in NetBeans. These videos are in Blackboard.
The Package
In Java, when you create a project, NetBeans gives you a folder called “Source Packages”. Inside the folder we create a specific package to hold more stuff. Think of “Source Packages” as your cellphone and the specific package you created as the contacts application on your phone. You have to open your contacts app in order to see the individual contacts within it.
When naming a package, use the reserves domain naming convention with all lowercase letters. In Program Logic, we only have one package and it should be named like this:
edu.northwoodtech.controller
The class
All Java source code is contained within a class. Using the contact app analogy, the class would be an individual contact card. In Java, classes always start with an uppercase letter and in Program Logic, we only use two classes Controller and Main. These are actually two different types of classes, so you must choose carefully. However, in both cases, when you create a Java class in NetBeans, the file extension of .java is automatically added, so in the project explorer of NetBeans, you would see Controller.java and Main.java.
Our standard is to explicity state the access level of a class. In Program Logic, that is always public. The access level refers to how visible the class is, ours is publicly available, which means the entire application has access to it.
Controller Class
The Controller class is a Java class. We always include a special method called a constructor in our Controller class. The constructor, says how we are going to build the class. For us, the code in the constructor states the first method we want to run. We write the bulk of our code in the Controller class.
public class Controller {
//no-argument constructor
public Controller(){
//we call a method here
}//end of constructor
}
Notice the name of the class and the name of the constructor are the same. That MUST be the case. But also notice how there signatures differ. The class uses the keywords public and class, however, the constructor only uses the keyword public but it includes parentheses.
Main Class
The Main class is a Java Main class. It differs from a regular Java class, because a Java Main class always include a main method. When you have NetBeans create a Java Main class, the main method is automatically included. We never change the signature of our main method. When you look at the project explorer in NetBeans, the Main.java file has a "play button" icon on it. The main method is where our program starts its execution.
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//we write one line of code here
}//end of main method
}//end of Main class
Methods
Getting back to our contacts analogy, a method is like the details you have for each contact. In Java, we camel case our method names. Additionally, the name should include an action that indicates the purpose of the method. For instance, a method named validatePhone() is better than a method named phone(). Furthermore, a method signature or call to a method always include parentheses.
Methods always go inside the curly braces of the class. We never nest a method inside of another method, so be careful where you place your methods. That is one reason why I suggest you label the ending curly braces of classes and methods.
Except for the main method, all our methods will have the access level of private. That means the are only known to other methods within the same class. However, unlike a class, all methods, except for the constructor, hava a return type. For now, that will be the keyword void, however, when we take a deep dive into methods, you will see that a method is capable of return data to its caller. In those cases, the return value is the data type of the variable you wish to return.
private void validatePhone(){
//write code in here
}//end method validatePhone()
Scope and Code Blocks
Did you notice that in all the code snippets above, there are curly braces? These braces denote a block of code; meaning all the code inside the curly braces go together. A block of code is sometimes called the code body. Our outter block is the class. The class contains one or more methods, and a method contains one or more lines and/or blocks of code. In Java, we always write code inside of a method and the method always goes inside of a class.
We declare our variables at the top of a method, after its opening curly brace. This makes the scope of variables limited to the method where they are declared. This limited scope is called local scope. You can declare variables so that their scope is global to the entire class, but this can be a dangerous way to code. Globally scoped variables have a place, but you need to understand their vulnerabilities and use them sparingly. I will let you know if/when it is okay to use global variables in my assignments.
The Semicolon
If we declare six variables on separate lines, we have six lines of code. Each declaration is a self contained statement and must be terminated with a semicolon. Any time we have a self contained code statement, such as a method code, or print statement, it is terminiated with a semicolon. The semicolon says, I'm done with this item for now. The semicolon must be used in Java and C#, however, there are rules for where not to use it.
Did you Know?
Our Java standard of starting a class with an uppercase letter is called Pascal case and it is also the standard for the Java programming language. Our Java standard of starting a method with a lowercase letter is called camel case and it is also the standard for the Java programming language. Knowing these standards help you read through unfamiliar Java source code. Reading source code helps you to be a better developer.
What You Learned
- What is a package and how to name one
- The purpose of a class and how to write one
- The difference between a Java class and a Java Main class
- The purpose of a method
- The difference between globally scoped and locally scoped variables
- The semicolon is a termination character
- Access levels and return types
Assignment Information
See Blackboard for assignment resources and assignment details.
What's next?
This is the last chapter in the Programming Basics Unit. If you need help or have questions, do not hesitate to contact me.