The first example defines a class named HelloJava.
public class HelloJava {
...
Classes are the fundamental building blocks of most object-oriented languages. A class
is a group of data items with associated functions that can perform operations on that data. The data items in a class are called variables, or sometimes fields; in Java, functions
are called methods. The primary benefits of an object-oriented language are this association
between data and functionality in class units and also the ability of classes to
encapsulate or hide details, freeing the developer from worrying about low-level details.
In an application, a class might represent something concrete, such as a button on a
screen or the information in a spreadsheet, or it could be something more abstract, such
as a sorting algorithm . A class representing a spreadsheet might, for example, have variables that represent the values of its individual cells and methods that perform operations on those cells, such as “clear a row” or “compute values.”
Our HelloJava class is an entire Java application in a single class. It defines just one
method, main() , which holds the body of our program:
public class HelloJava {
public static void main( String[] args ) {
...
It is this main() method that is called first when the application is started. The bit labeled
String [] args allows us to pass command-line arguments to the application. We’ll
walk through the main() method in the next section. Finally, we’ll note that although
this version of HelloJava does not define any variables as part of its class, it does use
two variables, frame and label, inside its main() method. We’ll have more to say about
variables soon as well.
0 comments:
Post a Comment