Home > front end >  How does Java know which constructor to use in a class?
How does Java know which constructor to use in a class?

Time:11-18

I am trying to understand OOP a little bit better. I was wondering how Java knows which constructor to use. for example I have a class below and 2 constructors. one which takes DBC values and another which takes some strings. Does it know just by that you pass through to it or am i understanding the whole thing wrong?

  public class EpsMockEmailInfo {
    
        private String email;
        private String emailType;
        private String emailDate;
        private String status;
        
        public EpsMockEmailInfo (String email,String emailType,String emailDate,String status) {
            this.email = email;
            this.emailType = emailType;
            this.emailDate = emailDate;
            this.status = status;
        }
    
        public EpsMockEmailInfo (Dbc dbc){
            email = dbc.getString(1);
            emailType = dbc.getString(2);
            emailDate = dbc.getString(3);
            status = dbc.getString(4);
            
        }
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getEmailType() {
            return emailType;
        }
    
        public void setEmailType(String emailType) {
            this.emailType = emailType;
        }
    
        public String getEmailDate() {
            return emailDate;
        }
    
        public void setEmailDate(String emailDate) {
            this.emailDate = emailDate;
        }
    
        public String getStatus() {
            return status;
        }
    
        public void setStatus(String status) {
            this.status = status;
        }
    
    }

CodePudding user response:

Match on method signature

A constructor is chosen by matching the number and types of its declared parameters against the number and types of your call’s arguments.

If you pass four String objects, then public EpsMockEmailInfo (String email,String emailType,String emailDate,String status) { … } is executed.

If you pass a single Dbc object, then public EpsMockEmailInfo (Dbc dbc) { … } is executed.

Formally, the combination of a method’s name with the number and types of its parameters is known as a method signature. Method signatures are unique within an app, meaning, you cannot write two methods with the same fully-qualified name and the same number and types of arguments; the compiler won’t allow. So the JVM always knows which method to call.

CodePudding user response:

In Java, the real name of a constructor or method, i.e., the identifier that is used to identify it and look it up, includes the number and types of the arguments. This is called the method or constructor signature.

Pretty much every use that you might imagine for the method name is actually based on the signature instead. Overloading, which is what we call using the same name for multiple distinct methods or constructors, exists only in our heads. In Java, foo(int) and foo(boolean) are as different from each other as they are from bar(int).

  • Related