-1
public class Client {
    public static void main(String args[]){
        Malo e = new Malo();
        e.setEid(11);
        e.setEsalary(3000);
        e.setEname("durga");
        System.out.println(e);
}
}

when i create an object and pass the parameter with dot operator it is not working.It says that constructor is not defined

public class Client {
    public static void main(String args[]){
       Malo e = new Malo(11,3000,"del");
       System.out.println(e);
}
}

but when i pass the parameter int the object creation line it works but why the above method is not working?

public class Malo {
        int eid;
        int esalary;
        String ename;
    
        //constructors
    
        public Malo(int eid, int esalary, String ename) {
            this.eid = eid;
            this.esalary = esalary;
            this.ename = ename;
        }
    
    
        public int getEid() {
            return this.eid;
        }
    
        public void setEid(int eid) {
            this.eid = eid;
        }
    
        public int getEsalary() {
            return this.esalary;
        }
    
        public void setEsalary(int esalary) {
            this.esalary = esalary;
        }
    
        public String getEname() {
            return this.ename;
        }
    
        public void setEname(String ename) {
            this.ename = ename;
        }
        
    
    
        @Override
        public String toString() {
            return "{" +
                " eid='" + getEid() + "'" +
                ", esalary='" + getEsalary() + "'" +
                ", ename='" + getEname() + "'" +
                "}";
        }
        
    }

This is my malo class and i declared as public

Deepak Silver
  • 27
  • 1
  • 3
  • You're asking about class `Malo`, but you don't show us the class?!? How are we to know what constructors the class has, if you don't show it to us. ** – Andreas Jul 02 '21 at 04:23
  • @Andreas you can probably take a _really_ good guess at what constructor the class _doesn't_ have. – Dawood ibn Kareem Jul 02 '21 at 04:26
  • @Andreas can you tell me the reason now? – Deepak Silver Jul 02 '21 at 04:42
  • @DeepakSilver Yeah, it's because you didn't declare a no-arg constructor. The compiler only creates a default no-arg constructor when there are no constructors. As soon as you declared a constructor, you have to explicitly declared a no-arg constructor if you still want one. – Andreas Jul 02 '21 at 06:20
  • [Why does the default parameterless constructor go away when you create one with parameters](https://stackoverflow.com/q/11792207/5221149) --- [Java default constructor](https://stackoverflow.com/q/4488716/5221149) --- [Difference between a no-arg constructor and a default constructor in Java](https://stackoverflow.com/q/27654167/5221149) – Andreas Jul 02 '21 at 06:24

1 Answers1

0

Because you don't have a zero parameter constructor on Malo, or if you do it isn't public.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127