You are on page 1of 2

Programming Notes: Class 1

Topics Covered:
 Structure of Java Program
Programming is an art similar to cooking, one must have a good knowledge of all the ingredients and their
features to prepare a delicious dish.

Structure of Java:
Follow the given structure to write any java program, since it increases the readability and makes it clear for
the interviewer about your good programming skills.

class ClassName { //Begin the class


Global Variables Declaration Section

User Defined Functions {

} //Use this block to Custom functions


public static void main(String[] args){

} //Main Function
} // End of Class

 Class: Class is the outer most covering of a java program which is used to encapsulate the global
variables, user defined functions, and the main function together. (This definition is not based on
OOPs).
 Global Variable Section: This section is used to declare the variables which can be accessed freely by
both users defined functions and the main function without any restrictions. (Real world example
would be public Roads, Railways etc. which can be openly accessed by all the citizens without any
restrictions.)
 User Defined Functions: In this section the Developer defines and creates his own functions to
perform a particular task.

Example: static void add(int a,int b)


{
System.out.println(“Sum=”, a+b);
}

The above function can be used to print the sum of any two numbers.

Pentagon Space Programming Data Structures and Algorithms


https://pentagonspace.in/ Technical Trainer : Pavan Prakash
 Main Function: This is the stomach of the java program, the execution of any program always start
from main section. This can be called as the soul of any program.

public static void main(String[] args){

add(10,20);
}

There 4 sections are the heart and soul of Java programs. The one who knows to use them optimally will
design and develop one of the best programs in the future.
Example Program:

class SumOfTwoNumbers{ //Start of class


static int a,b; //global variables
static void add(){ //User Defined Function
a=10;
b=20;
System.out.println(“Sum=” + (a+b));
} //User Defined Function ends here
public static void main(String[] args){ //Start of Main Section
add(); //Calling the user defined function
} //End of Main Section
} //End of class

Output:

Sum=30

Pentagon Space Programming Data Structures and Algorithms


https://pentagonspace.in/ Technical Trainer : Pavan Prakash

You might also like