Object Oriented Programming: Unit I: Introduction to OOP and Java

Structure of a Java Program

The documentation section provides the information about the source program. This section contains the information which is not compiled by the Java. Everything written in this section is written as comment.

Structure of Java Program

• The program structure for the Java program is as given in the following figure –

Documentation section: The documentation section provides the information about the source program. This section contains the information which is not compiled by the Java. Everything written in this section is written as comment.

Package section: It consists of the name of the package by using the keyword package. When we use the classes from this package in out program then it is necessary to write the package statement in the beginning of the program.

Import statement section: All the required java API can be imported by the import statement. There are some core packages present in the java. These packages include the classes and method required for java programming. These packages can be imported in the program in order to use the classes and methods of the program.

Class definition section: The class definition section contains the definition of the class. This class normally contains the data and the methods manipulating the data.

Main method class: This is called the main method class because it contains the main() function. This class can access the methods defined in other classes.

Writing Simple Java Program

Any Java program can be written using simple text editors like Notepad or Wordpad. The file name should be same as the name of the class used in the corresponding Java program. Note that the name of the program is Firstprog and class name is Firstprog. The extension to the file name should be java. Therefore we have saved our first program by the name Firstprog.java.

The output of this program can be generated on the command prompt using the commands

javac filename.java

java filename

The sample output is as shown below for the program Firstprog.java

Program explanation

• In our First Java program, on the first line we have written a comment statement as

/*

This is my First Java Program

*/

This is a multi-line comment. Even a single line comment like C++ is also allowed in Java. Hence if the statement would be

//This is my First Java Program

• Is perfectly allowed in Java. Then actual program starts with

class Firstprog

Here class is a keyword and Firstprog is a variable name of class. Note that the name of the class and name of your Java program should be the same. The class definition should be within the curly brackets. Then comes

•  public static void main(String args[])

This line is for a function void main(). The main is of type public static void.

The public is a access mode of the main() by which the class visibility can be defined. Typically main must be declared as public.

The parameter which is passed to main is String args[]. Here String is a class name and args[] is an array which receives the command line arguments when the program runs.

 System.out.println("This is my first java program");

To print any message on the console println is a method in which the string "This is my first java program" is written. After println method, the newline is invoked. That means next output if any is on the new line. This println is invoked along with System.out. The System is a predefined class and out is an output stream which is related to console. The output as shown in above figure itself is a self explanatory. Also remember one fact while writing the Java programs that it is a case sensitive programming language like C or C++.

Object Oriented Programming: Unit I: Introduction to OOP and Java : Tag: : - Structure of a Java Program