Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How does Java know which constructor to use in a class?

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;
        }
    
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading