Java Fundamentals

Structure of Java Program

The structure of a Java source file is the order in which the elements are placed. They are as follows :

  • A package declaration specifies to the compiler that the classes defined here are a part of this package.
  • The import statement informs the interpreter to load the classes in that package. The import statements help us to access the classes of other packages.
  • Any number of top-level class or interface declarations are allowed. Since the declarations are for the same package we refer to them as top-level which is the package level.

The classes and interfaces can be defined in any order and are collectively referred as type declarations.

Writing a program

A Java program is a collection of one or more classes with one of them containing the program's starting point. According to the rule only one class in the entire program can have public accessibility. The program file should be named as this class name followed by .java extension. Each class in a program is compiled and then a separate class file is created with the name of the class followed by .class extension. This class file contains Java byte code.

Here's your first program in Java. Try it out and see whether you get the same output or not.

public class ProgOne
{
public static void main(String[] args)
{
System.out.println("My first program !");
}
}

Declaring a Class : The first line

public class ProgOne

declares a class. Since Java is an object-oriented language, everything will be placed inside a class. This class has been given public accessibility, hence the file name will be ProgOne.java. The class begins with an opening brace '{' and ends with a closing brace '}'.

The main( ) method :

public static void main(String[] args)

This line declares a method named main. The Java interpreter or the Java Virtual Machine(JVM) begins the execution of a program from the main( ) method.

The main( ) method has public accessibility i.e. it is accessible from any class. The keyword static denotes that the main( ) method belongs to the class, the keyword void indicates that no values will be returned from this method. A string array args is passed as a parameter to pass on the information to the main( ) method during the program execution.

Displaying the Output :

System.out.println("My first program !");

This line helps us to display the message "My first program !" on the screen. Anything written between " " will be displayed.

Compiling and Running the program :

Before running a program it is necessary to compile it. To compile a program in command line you must write 'javac' leave a space and write the program name along with its extension.

>javac ProgOne.java

Now a class file ProgOne.class is created containing the Java byte code. To run the program just type java and the program name.

>java ProgOne

Output :

My first program !

Object Creation

Now that you have written your first program, lets see how we can create and use objects in a program.

class Squares
{
       int result;
       void calculate(int num)
       {
              result = num * num;
       }
}


public class FindSquares
{
       public static void main(String[] args)
       {
              int answer;

              Squares sq1 = new Squares();       // Creates an object sq1
              sq1.calculate(5);
              answer = sq1.result;
              System.out.println("Answer = " + answer);
       }
}

In the above program we declare two classes Squares and FindSquares. The Squares class has a variable result and a method calculate which finds the square of a number and stores it in result. The FindSquares class contains the main( ) method in which we have a variable answer. The next line creates an object sq1 of class Squares. The data is passed to the calculate( ) method which processes the data. The object accesses the value of result and stores it in answer. Thus object sq1 has access to the variables and methods of class Squares.

Note : Java 2 SDK permits only one public class definition in a source file. The file name should be same as that of the public class. eg. If public class name is FindSquares, then the file name will be FindSquares.java.

 

 
Home Topics Problems Feedback About Us Home Topics Problems Feedback About Us