java - Why am I being asked for a return type when creating a constructor? -


i'm trying make class , create 2 constructors in it. i've created have done of previous classes , constructors, reason keeps telling me add return type 2 constructors.

i've tried see if i've created different previous constructors can't see difference.

can see i'm going wrong here?

public class book {      //instance variables     //accessspec type varname;     private string title;     private string author;     private double price;      //constructors     public initialiseinstancefields() {         title = "";         author = "";         price = 0.00;     }      public initialiseinstancefields(string titlein, string authorin, double pricein) {         title = titlein;         author = authorin;         price = pricein;     }       //methods     //accessspec returntype varname(arglist){}     //return title     public string gettitle() {         return title;     }  }//end class 

constructors must have same name class name. initialiseinstancefields normal method , not constructor , hence return type required. if want treated constructor redefine using name of class i.e book change constructors definition as:

public book() {     title = "";     author = "";     price = 0.00; }  public book(string titlein, string authorin, double pricein) {     title = titlein;     author = authorin;     price = pricein; } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -