Each class in a Java project can have a main method, but only one main method is executed, and the other main methods can unit test the class.
Public classStatictest { Public Static voidMain (string[] args) {employee[] staff=NewEmployee[3]; staff[0]=NewEmployee ("Tom", 40000); staff[1]=NewEmployee ("Dick", 60000); staff[2]=NewEmployee ("Harry", 65000); for(Employee e:staff) {E.setid (); System.out.println ("Name=" +e.getname () + ", id=" +e.getid () + ", salary=" +e.getsalary ()); } intn=Employee.getnextid (); System.out.println ("Next available id=" +N); }}classemployee{ PublicEmployee (String N,Doubles) {name=N; Salary=s; ID=0; } PublicString GetName () {returnname; } Public Doublegetsalary () {returnsalary; } Public intgetId () {returnID; } Public voidsetId () {ID=NextID; NextID++; } Public Static intGetnextid () {returnNextID; } Public Static voidMain (string[] args) {Employee e=NewEmployee ("Harry", 50000); System.out.println (E.getname ()+" "+e.getsalary ()); } PrivateString name; Private Doublesalary; Private intID; Private Static intNextid=1;}
Java employee can get
Harry 50000.0
Java Statictest can get
name=tom,id=1,salary=40000.0
name=dick,id=2,salary=60000.0
name=harry,id=3,salary=65000.0
Next available id=4
Every class in a Java project can have a Main method