Sunday, Dec 10th

Last update12:59:40 PM GMT

Java Refresher II

Write e-mail

Java RefresherA Java program is nothing but mostly a collection of objects communicating to other objects by invoking each other's methods. Every object is associated with a certain type, and that type is defined by a class or an interface. And at most of the times, you will see collection of different type of objects.

argaiv1865

  • Class: A blueprint that describes the state and behaviour, which the objects of this class type share.
  • Object: At runtime, when the Java Virtual Machine (JVM) encounters the new keyword, it will use the appropriate class to make an object which is an instance of that class. That object will have its own state, and access to all of the behaviours defined by its class.
  • State (instance variables): For each created object (instance of a class) in a program, there will be a unique set of instance variables as defined in the class, and just for that object. Collectively, the values assigned to an object's instance variables make up the object's state.
  • Behaviour (methods): When a programmer creates a class, she defines the behaviour of that class, in form of methods. All the business logic or class’ logic is stored in these methods. They work on execution of the algorithms on the instance variables data.

A simple java program is shown below, to give you more clear idea about classes, objects and methods. Here, we have considered an example of a student, which can be represented by states like name and score. One of the behavior of class can be calculating average score of a student, which is defined by the method getAvgScore().

public class Student // Now, this is the blueprint/template
{
     private String name; // One of the instance variable, also known as state of the object
 
     private int[] score; // Array of the marks scored by the student
 
     public String getName() { // Public method to read the private instance variable "name"
          return name;
     }
 
     public void setName(String name) {
          this.name = name;
     }
 
     public int[] getScore() {
          return score;
     }
 
     public void setScore(int[] score) {
          this.score = score;
     }
 
     public int getAvgScore()  // Method to calculate the average score of a student 
     {                                  // This method contains the logic to calculate average score
          int avgScore, totalScore=0 ;
 
          for(int i=0 ; i<score.length ; i++)
               totalScore += score[i];
 
          return (totalScore/score.length) ;
     }
 
     public static void main(String[] args) {     // Main method to run the program
          Student std = new Student();     // Declaring and initializing an object/instance of the class
          std.setName("TRIAD") ;           // Setting the name of the student
 
          // Setting the marks of that student through int array
          std.setScore(new int[]{76, 81, 79, 93, 69});
 
          // Prints the average score of the student
          System.out.println(std.getName() + " got average score of " + std.getAvgScore());
     }
}
 

Tags:

Share this post



Add comment

Please refrain from using slang or abusive language in the comments.
To avoid waiting for your comment to be published after being reviewed, please log in as a registered user.


Security code
Refresh

Web Hosting