简体   繁体   中英

is there a way to use a constructor and a mutator in java together

The code is incomplete at the moment, but I was wondering if I could use the String partNo the one that the constructor is making inside the mutator.

public static class Invoice{
    Scanner sam = new Scanner(System.in);
    int Quality;
    double price;
    public Invoice(){ 
        
    }
    public Invoice(String partNo, String description) { 
    }
    public void setPartNo() {
        System.out.println("Enter part no: ");
        String partNo  = sam.nextLine();
        
    
    }
    public static String getPartNo() {
        return Invoice.getPartNo();
        
    }
        }
    

    public static void main(String[] args) {
      Invoice.getPartNo();
public static class Invoice{
    Scanner sam = new Scanner(System.in);
int Quality;
double price;
public Invoice(){ 
    
}
public Invoice(String partNo, String description) { 
}
public void setPartNo() {
    System.out.println("Enter part no: ");
    String partNo  = sam.nextLine();
    

}
public static String getPartNo() {
    return Invoice.getPartNo();
    
}
    }


public static void main(String[] args) {
  Invoice.getPartNo();

You here have a few parameters to your constructor, you don't do anything with, which is pretty pointless. They won't exist anymore once the constructor has finished executing, so it's impossible for your mutators to get them

Those parameters, set them up as instance members, that way your mutators will also have access to them.

public static class Invoice {
  Scanner sam = new Scanner(System.in);
  int quality;
  double price;

  // add the additional instance members
  String partNo;
  String description;

  public Invoice() { }

  public Invoice(String partNo, String description) {
    this.partNo = partNo;
    this.description = description;
  }

  // a normal mutator (setter) will not use a Scanner, but accept the value as parameter
  // but for now, I'm keeping your original design
  public void setPartNo() {
    System.out.println("Enter part no: ");
    this.partNo = sam.nextLine(); 
    // again, use this.partNo. If you declare it as a String here, it will no longer exist when the method is finished
  }

  // this getter is both pointless, and will cause trouble by creating an endless 
  // recursive loop. Delete it.
  public static String getPartNo() {
    return Invoice.getPartNo();
  }
}

Now, the same code with some improvements:

public static class Invoice {
  int quality;
  double price;

  // add the additional instance members
  String partNo;
  String description;

  public Invoice() { }

  public Invoice(String partNo, String description) {
    this.partNo = partNo;
    this.description = description;
  }

  public void setPartNo(String partNo) {
    // you can always add validation on the new value here
    this.partNo = partNo;
  }

  public String getPartNo() {
    return partNo;
  }
}

this, you will be able to use as follows:

public static void main(String[] args) {
  Invoice in = new Invoice("part", "description");
  System.out.pintln(in.getPartNo());
  in.setPartNo("new PartNo");
  System.out.println(in.getPartNo());
}

If you are wondering whether your getter should be static: it should only be static if the field is static itself. Since you set the value by a parameter you pass to the constructor, it is doubtfull it should be static.

A parameter to the constructor should (usually) only set the value for that particular instance. If it's a static field, it will be set for every single instance of that type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM