Structure for java program

Structure and Members of the Java Program

When we are writing any program in any language we need to follow a standard structure for writing the program which is recommended by the language experts. A java program may contain many classes of which only one class will have a main method. Class will contain data members and methods that operate on the data members of the class. To write a Java program, we first need to define classes and then put them together. Generally a standard java program consists of following blocks as shown in below figure.

Explanation:
1. Package is a collection of classes, interfaces and sub packages. In a java program if we are using any pre-defined classes and interfaces then it is the responsibility of the java programmer to import that particular package containing such specific classes and interface. In java by default java.lang.* package is imported by every program.
2. Class is a keyword used for developing user defined data types. Every java program must starts with a prototype of class. The class has been declared public, means all classes can access the class from all packages. Generally, however, we will declare classes in java without specifying a modifier.
3. Class name is the name given to that class. Every class name is treated as one kind of user defined data type.
4. Data Members represents either instance members or static members.
5. Constructor function is called when an object of the class is created. It is a block of code that initializes the newly created object. The constructor simply has the same name as the name of the class name. A constructor does not have a return type. A constructor is called automatically when a new instance of an object is created. In the following code, the constructor bird() prints a message.

When we create the object of the bird class as shown above:
bird b = new bird();
The new keyword here creates the object of class bird and invokes the constructor to initialize this newly created object.
Constructor and method are different because the constructor is used to initialize the object of a class while the method is used to perform a task by implementing java code. Constructors cannot be declared as abstract, final, static and synchronized while methods can be declared. Constructors do not have return types while methods do.
6. User-defined methods represent either instance (or) static and they will be selected depends on the class name and these methods are used for performing the operations either once (or) repeatedly. All the user-defined methods of a class contain logic for a specific problem. These methods are known as Business logic methods.
7. All java program starts its execution with main() method so main() method is known as the backbone of the program. The Java Virtual Machine starts running any java program by executing main() method first.
8. Java’s main() method is not returning any value so its return type must be void.
9. Also main() method executes only once throughout the life of the Java program and before the object creation so its nature must be static.
10. The main() method is accessed in all the java programs, its access specifier must be public (universal).
11. Each and every main() method of java must take an array of objects of String class as an argument.
12. The block of statements are set of executable statements written for calling user-defined methods of the class.
13. If we have multiple java files then the naming convention of class file in java is that, whichever class is containing main() method, that class name will be given as the file name with an extension (dot) .java.
Types of Data Members:
Java Class is a collection of data members and functions. Any java program may contain two types of data members. They are;
1. Instance or non-static data members
2. Static or class data members
The following table describes the difference between the two.

Читайте также:  String tostring method in java

Types of Methods:
In java program generally we may define two types of methods apart from constructor. They are;
1. Instance or non –static methods
2. Static or class methods
The following table describes the difference between the two.

The following example named TestGVP.java demonstrates the use of different members of the java class.

Источник

Basic Structure of Java Program with Example

Basic Structure of Java Program

It is optional to write in your Java program. Note that we can not use more than one package statement in our Java program. It should be mentioned before any Interface and class definition. package keyword is used for declaring the package name in the program.

Documentation Section

It is also an optional section to write but it is important to write for better readability and understanding. It contains basic information about the class, variables, methods, author’s name, program name, version, company name, date of creation, etc. The compiler does not execute of contents of the documentation section and it ignores the statements written in it. We use comments to write in it and comments can be single-line, multi-line, and documentation comments. For writing single-line comments we use (//) and for multi-line comments, we start with (/*) and end with (*/). Documentation comment starts with (/**) and end with (*/).

Example single-line comment:

// This example for writing single line comment.

Example multi-line comment:

/* It is an example for writing

Example documentation comment:

/** It is an example for documentation comment */

Import Statements

It contains predefined classes, methods, interfaces. For using that predefined classes or interfaces in your program you will have to import that classes and with the help of “import” keyword, we can do that. Keyword “import” imports the classes defined in different packages.

Читайте также:  Store string in java array

It is written between classes declaration and package statements. With the help of the import keyword, we can import a specific class or all the classes of that package.

Class Definition

It is one of the most important sections of Java program and is necessary to write. Without a class, you can not create a Java program. At least one class must be present in your java program. You can create more than one class. For defining a class we use “class” keyword and after that, we write the name of the class you want to give. Given below is the example for defining a class-

Interface Statements

It is also an optional statement to write. If you want to use an interface then you can create it with the help of “interface” keyword. Interface contains methods declaration and constants. It does not contain the method definition and it can not be instantiated. Classes uses interfaces with the help of “implement” keyword and interface uses another interface with “extends” keyword. Given below is the example for creating an interface:

Methods

It is also an optional section. If you want use a method in your program for particular functionality then you can define it. Methods are created for performing a specific task and to avoid the rewriting of the same code. Methods can be created with the private, public, protected and default depends on your need and accessibility in your program. Given below is an example for creating a method-

Main Method Class

It is the most important section of your Java program. We define the main() method in this section and without this Java program would not be able to execute. Execution of Java program starts from main method. It is written inside a class and inside main() method we call methods and instantiate classes. Given below is the structure of main method and it must be written as it is.

Читайте также:  Java error public static void main string args

Example Program:

Given below is an example of a complete Java program for the addition and multiplication of two numbers with the use of the above sections.

Various parts of the above program are described below:

Import java.util.Scanner

The statement is importing the Scanner class present in the util package. Scanner class is used for getting user input.

public class example

The above statement is written for the definition of class named “example” having “public” access modifier.

public void add()

It is a method defined for the addition of two numbers having “public” access modifier.

public void multiply()

It is another method defined for multiplication of two numbers having “public” access modifier.

public static void main (String []args)

It is main( ) method whenever compiler executes our program it always starts from main( ). The keyword static is used for calling the main( ) method without creating it’s object. The method is made public for accessing the main ( ) method from another package. Keyword “void” means it does not return any value. In arguments args[] array is passed of String data type and used for taking input parameter if your program run through console and “public” is its access modifier.

System.out.println()

The line is used to print the text where System is predefined class and out is an object of printWriter class. The method println() is for printing the text as output on screen with new a line.

Источник

Оцените статью