2

I have put together the constructor below. A question that I have is: how do I use the same constructor with no arguments and with two or three at the same time? Is there more than one way to do this? Thanks

public class bankaccount {

  String firstnameString;
  String lastnameString;
  int accountnumber;
  double accountbalance;;

  public bankaccount(String firstname,String lastname){
    int accountnumber=999999;
    double accountbalance=0.0;
  }
}
4
  • Just define multiple constructors. Commented Feb 20, 2013 at 15:08
  • 1
    You are declaring local variables in your current constructor, whereas you probably want to set the value of your fields. You have to fix that... Commented Feb 20, 2013 at 15:21
  • @jlordo he also doesn't use firstname or lastname Commented Feb 20, 2013 at 15:22
  • @ColinD: Yes, that needs to be fixed, too. Commented Feb 20, 2013 at 15:23

3 Answers 3

6

You need to implement all the variants you want to use. You can then use this() to call between constructors, to avoid code redundancy:

public class BankAccount {

  public BankAccount(){
     this("", "");
     // or this(null, null);
  }

  public BankAccount(String firstname){
     this(firstname, "");
     // or this(firstname, null);
  }

  public BankAccount(String firstname, String lastname){
      // implement the actual code here
  }
}

By the way, you should check out the Java Coding Conventions - class names (and therefore constructors) are noted in camel case.

Sign up to request clarification or add additional context in comments.

2 Comments

Stylistic point - I would suggest null would be a more appropriate default value. But each to their own...
@DuncanJones Agreed, my mind was wildly jumping between these alternatives while typing :) At the end, it basically depends on what the "final" constructor does
0

If you do not implement any constructors, the no argument constructor is provided for you. If you want to have multiple constructors, you much implement all of them yourself.

public class bankaccount {

  String firstnameString;
  String lastnameString;
  int accountnumber;
  double accountbalance;;

  public bankaccount(String firstname,String lastname){
    int accountnumber=999999;
    double accountbalance=0.0;
  }

  public bankaccount(){
    // put code here, or call this(null,null) / a different constructor
  }

}

1 Comment

Both constructors have the same effect.
0

The others suggested you to create multiple constructors, and that's fine. However, there are situations in which you may prefer to have only the zero-argument constructor and use getters and setters to access the properties.

Your BankAccount class might be one of these situations. It seems like a data object, one of that objects that are persisted into the DBMS by using some ORM (e.g. Hibernate). Hibernate does not need a multi-argument constructor, it will call the zero-argument constructor and access the properties via getters and setters.

What is this class for? Is it a database entity mapped to an object? Do you really need all these costructors (it may be a waste of time)?

Theoretically, my suggestion can be considered a fight against the good practices of OO design, but practice is somewhat different from theory. If your class just carries some data and your constructors perform no checks on the validity of the provided parameters, than you can just list the properties and use the facilities of your IDE to create getters and setters.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.