Guyz in this session we are discussing the Structure of a Java program. The structure of a java program is an important aspect of the overall design of the application. The java program structure is divided into various sections, namely package statement, import statement, interface statement, class definition, and main
method class.
so this statement is optional.
once all the instruction in the main method is executed, the control is transferred out of the class thoughts terminating the entire program..
Any Java program can be written using simple text editor notepad or WordPad. The file name should be same as the name of the class used in the corresponding java program. The extension of the file name should be .Java.
Myprogram.java
/*This is my first java program*/
class myprogram
{
public static void main(string args[])
{
system.out.println("my first program");
}
}
In our first java program, on the first line, we have written a comment statement. This is the multi-line comment.
Then the actual program starts with class myprogram.
Here, class, is the keyword and myprogram is the variable name of the class. The class definition should be within curly braces.
.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 an access mode of a main()
by which the class visibility can be defined. Typically main must be declared as public.
The parameter which is passed to main string args[].hence string is class name and args[] is an array which receives the comment line arguments when the program runs.
System.out.println("my first program");
To print any message on a console println in a method in which the string "my first program" is written.
After the println method,a new line is invoked.java program is a case sensitive programming language
like c or c++.
Package Statement
|
Import Statement
|
Interface Statement
|
Class definition
|
Main method class |
Package Statement:
It is the first statement in java program that tells the compiler that all the classes defined in the file belong to this package. For example, consider this statement.'Package employee;'
Here, the employee is the name of the package. It is not necessary that our classes are part of the package,so this statement is optional.
Import Statement:
It allows us to access a class which belongs to some other package. For example, consider this statement.'import employee.'ename; Here,employee is the name of the package and ename is the class which you want to access. There can be a number of import statements in a program.Interface Statement:
An interface is a way for implementing multiple inheritances in java. It is just like a class but it contains only method declarations.Class Definition:
The class is the most important element of a program. A program may consist of any number of class definitions.Main Method class:
It is an essential part of a java program as it contains the main method which is the starting point of a program. Inside the main method class, the objects of several classes can be created, accessed and manipulated.once all the instruction in the main method is executed, the control is transferred out of the class thoughts terminating the entire program..
Example:
Any Java program can be written using simple text editor notepad or WordPad. The file name should be same as the name of the class used in the corresponding java program. The extension of the file name should be .Java.Myprogram.java
/*This is my first java program*/
class myprogram
{
public static void main(string args[])
{
system.out.println("my first program");
}
}
Explanation:
In our first java program, on the first line, we have written a comment statement. This is the multi-line comment.
Then the actual program starts with class myprogram.
.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 an access mode of a main()
by which the class visibility can be defined. Typically main must be declared as public.
The parameter which is passed to main string args[].hence string is class name and args[] is an array which receives the comment line arguments when the program runs.
System.out.println("my first program");
To print any message on a console println in a method in which the string "my first program" is written.
After the println method,a new line is invoked.java program is a case sensitive programming language
like c or c++.
No comments:
Post a Comment