saving . . . saved about assignment of static block tutorial has been deleted. about assignment of static block tutorial has been hidden .
about assignment of static block tutorial
Title
Question
when that assignment program is executed, then it shows the number of cars in and out are increased by one only with all 4 objects. I think, the reason is may be the static block increases number of cars in and out by taking its global/default values each time when program is executed.
So my question is, this output is correct or not? i want to verify it.
Assignment program:
///CarService Program.
public class CarService {

    private static String Name;
    private static int No_of_Cars_in=0, No_of_Cars_out=0;
    private String make, model, status;
    private int regno;
    
    public CarService(String make, String model, String status, int regno) {
       
        this.make = make;
        this.model = model;
        this.status = status;
        this.regno = regno;
    }
    static {
        Name="CSS";
        No_of_Cars_in++;
        No_of_Cars_out++;
    }
    public void show() {
        System.out.println(Name+" "+No_of_Cars_in+" "+No_of_Cars_out+" "+make+" "+model+" "+status+" "+regno);
    }

}
/////Demo program.
public class Demo {
public static void main(String[] args) {
    CarService s1=new CarService("02Jun","12Re4","in",293);
    CarService s2=new CarService("14Nov","Rw45","in",2389);
    CarService s3=new CarService("10Jan","Yo829","in",38492);
    CarService s4=new CarService("25Jully","GH092","in",2038);
    
    s1.show();
    s2.show();
    s3.show();
    s4.show();
}
}
////And output is:
CSS 1 1 02Jun 12Re4 in 293
CSS 1 1 14Nov Rw45 in 2389
CSS 1 1 10Jan Yo829 in 38492
CSS 1 1 25Jully GH092 in 2038

Java General None min None sec 25-06-21, 3:02 p.m. Tanmay_Avinash_Patil

Answers:

The assignment for tutorial on "static block" tutorial is continuation to assignment for the tutorial on "static methods".
As per requirements of the tutorial on "static methods", it is necessary to define "service" method, which updates the No. of cars in for service and out after service.

By considering the requirements, a meaningful program can be written as follows.

/* CarService Class */
public class CarService {

 private static String Name;
 private static int No_of_Cars_in, No_of_Cars_out;
 private String make, model, status;
 private int regno;

static {
        Name="CSS";
        No_of_Cars_in=0;
        No_of_Cars_out=0;
 }
 
 public CarService(String make, String model, String status, int regno) {
    this.make = make;
    this.model = model;
    this.status = status;
    this.regno = regno; 
 }

 public void service() {
 No_of_Cars_in++;
 this.status="out";
 No_of_Cars_out++;
 }
 
public void show() {
    System.out.println(Name+" "+No_of_Cars_in+" "+No_of_Cars_out+" "+make+" "+model+" "+status+" "+regno);
 }

}

/* Demo Class */

public class Demo {
public static void main(String[] args) {
    CarService s1=new CarService("02Jun","12Re4","in",293);
    
    CarService s2=new CarService("14Nov","Rw45","in",2389);
    
    CarService s3=new CarService("10Jan","Yo829","in",38492);
    
    CarService s4=new CarService("25Jully","GH092","in",2038);
      
    s1.service();
    s1.show();
    
    s2.service();
    s2.show();
    
    s3.service();
    s3.show();
    
    s4.service();
    s4.show();
  }
}

/* Output */
CSS 1 1 02Jun 12Re4 out 293
CSS 2 2 14Nov Rw45 out 2389
CSS 3 3 10Jan Yo829 out 38492
CSS 4 4 25Jully GH092 out 2038

Please note that static block initializes No. of cars in for service and out after service to zero, and  the "service" method updates it after invoking on the object.


16-07-21, 12:50 p.m. pankajap@cse.iitb.ac.in


Log-in to answer to this question.