Uploaded by Jamarr Edwards

2.1 The Parts of a Java Program

advertisement
2.1 The Parts of a Java Program
Saturday, October 24, 2020
9:40 PM
// This is a simple Java program.
public class Simple
{
public static void main(String[] args)
{
System.out.println("Programming is great fun!");
}
}
From <https://media.pearsoncmg.com/ph/esm/0media_ecs/gaddis-java/pr2-1/1.html>
public class Simple
•
•
•
•
•
This line is known as a class header, and it marks the beginning of a class definition. One of the uses of a class is
to serve as a container for an application. As you progress through this book, you will learn more and more about
classes. For now, just remember that a Java program must have at least one class definition. This line of code
consists of three words: publi c, cl ass , and Si m pl e . Let’s take a closer look at each word.
publi c is a Java key word, and it must be written in all lowercase letters. It is known as an access specifier, and it
controls where the class may be accessed from. The publi c specifier means access to the class is unrestricted.
(In other words, the class is “open to the public.”)
cl ass , which must also be written in lowercase letters, is a Java key word that indicates the beginning of a class
definition.
Si m pl e is the class name. This name was made up by the programmer. The class could have been called Pi z za ,
or Dog , or anything else the programmer wanted. Programmer-defined names may be written in lowercase letters,
uppercase letters, or a mixture of both.
In a nutshell, this line of code tells the compiler that a publicly accessible class named Si m pl e is being defined.
Here are two more points to know about classes:
You may create more than one class in a file, but you may have only one publi c cl ass per Java file.
When a Java file has a publi c class, the name of the public class must be the same as the name of the file
(without the .java extension). For instance, the program in Code Listing 2-1 has a publi c cl ass named Si m pl e , so
it is stored in a file named Simple.java.
From <https://revel-ise.pearson.com/courses/5f4409b4f963b8001a64c23c/pages/a856f593996a6a118584551f45d59fabbabc5c0de?source=contents>
From <https://revel-ise.pearson.com/courses/5f4409b4f963b8001a64c23c/pages/a856f593996a6a118584551f45d59fabbabc5c0de?source=contents>
This line is known as a method header. It marks the beginning of a method. A method can be thought of as a group of one or more programming statements that
collectively has a name. When creating a method, you must tell the compiler several things you should be concerned about is that the name of the method is main,
and the rest of the words are required for the method to be properly defined.
New Section 1 Page 1
Download