Writing a Simple Java Program

Here we will write a simple program in Java.

Pre-requisites :-

  1. Java installed on your system.
    Download Java JDK and add JAVA_HOME environment variable to your system. Add ‘JAVA_HOME/bin’ path to your system’s PATH variable.
  2. IDE for Java- like eclipse.

Assuming that you are done with the above requirements. Lets start with our first Java program.

Step 1 – Create a new Project in your IDE.

A Project will have all the Java code related to a single project.

Step 2 – Create a new Class inside the created project.

A Class in Java defines the functionality and state for Objects of that class.

Class  – Class is Just a definition for functionality and state.
Object – An Object is an instance of a Class which actually holds states and provide functionalities. A Class can have multiple instances.

We will create a new Class ‘Student.java’ as follows:-

public class Student {

}

The IDE will generate an empty class. Let us add some state to the class – ‘name’ and ‘age’. ‘name’ will store the name of the Student and ‘age’ will store the age of the Student.

public class Student {

    private String name;
    private int age;
    
}

Now that we have defined state in our class, let us add behavior to our class. A simple behavior like – print the details for any Student instance. For adding behavior, we need to add methods in the class. Let us add a method printDetails() in our class as follows:-

public class Student {

    private String name;
    private String age;

    public void printDetails() {
        System.out.println("Student name=" + name + ", Student age=" + age);
    }
}

We have defined the state and behavior for our Student class. Now, how to create Objects of type Student and assign value to name and age variables?

Here, a Constructor comes into picture.

Constructor – A constructor is a method in a Java Class which is invoked when an Object is created.
Rules for writing a constructor :-

  1. Name of the Constructor should be same as the Class name.
  2. Constructor should not have any return type.

The constructor of our Student Class will take the two values name and age and assigns it to the instance variables.

public class Student {

    private String name;
    private String age;

    public void printDetails() {
        System.out.println("Student name=" + name + ", Student age=" + age);
    }

    public Student(String name, String age) {
        this.name = name;
        this.age = age;
    }
}

Now our Student class is ready for use. Let us write main method to execute our first Java program. Java invokes the main method to execute your application. In our main method, let us create 2 Student instances and print their details.

public class Student {

    private String name;
    private String age;

    public void printDetails() {
        System.out.println("Student name=" + name + ", Student age=" + age);
    }

    public Student(String name, String age) {
        super();
        this.name = name;
        this.age = age;
    }

    public static void main(String[] args) {
        // Create Student instance with name Rajesh and age 18
        Student s1 = new Student("Rajesh", "18");

        // Create Student instance with name Ankit and age 15
        Student s2 = new Student("Ankit", "15");

        // Call printDetail method on the instances.
        s1.printDetails();
        s2.printDetails();

    }
}

Run the Student Class. Following will be the output produced on console:-

Student name=Rajesh, Student age=18
Student name=Ankit, Student age=15

In this tutorial, we have seen how to write a simple java program and touched the concepts like:-

  1. Creating Class – state/behavior
  2. Object vs Class difference
  3. Declaring variables in a Class
  4. Writing methods in a Class
  5. Writing Constructor
  6. Writing Main method
  7. Creating Objects(instances of a Class)
  8. Invoking methods on Objects

In the next tutorials, we will see more detailed explanation of the above concepts.

 

Leave a Reply

Your email address will not be published. Required fields are marked *