3

For an assignment, we need to create a simplified BankAccount class with unique IDs for each object created from it. It would seem like the best means to do so would be a static int belonging to the class itself, but attempts to increment it are not increasing its value from 0. What mistake am I making here? I assume it's likely something trivial, but I can't seem to see it.

public class BankAccount {

// instance fields

/**
 * each BankAccount instance should have
 * a unique account number
 */

private int accountNumber;
private String accountHolder;
private double currentBalance;
private double overdraftLimit;

// static fields

private static int nextID;

// constructors

public void BankAccount(){
    this.accountNumber = BankAccount.nextID++;
    this.currentBalance = 0;
    this.overdraftLimit = 0;
}

public void BankAccount(String accountHolder, double overdraftLimit){
    this.accountNumber = BankAccount.nextID++;
    this.currentBalance = 0;
    this.accountHolder = accountHolder;
    this.overdraftLimit = overdraftLimit;
}
}

I defined a main method solely to test the object definition; it's superfluous to the class itself. Any help would be greatly appreciated!

EDIT: For reference, not a duplicate of the other linked issue. This concerns a badly initialised constructor, not a for loop.

3
  • 2
    You've posted over 100 lines of code, with little indication of what you're actually seeing. Please edit this to be a short but complete program demonstrating the problem. Commented Oct 14, 2013 at 19:04
  • What is the output that you are getting, Kyle? Commented Oct 14, 2013 at 19:05
  • @SimonAndréForsberg Sorry sorry. It was completely my fault. Damn me :( :( I'll remove my comments. Commented Oct 14, 2013 at 19:15

3 Answers 3

8

It's not the constructors

// this is method: 'bankAccountInstance.BankAccount()' 
public void BankAccount() 
{
    this.accountNumber = BankAccount.nextID++;
    //
}

// and this is method: 'bankAccountInstance.BankAccount("str", 5.1)' 
public void BankAccount(String accountHolder, double overdraftLimit)
{
    this.accountNumber = BankAccount.nextID++;
    //
}  

That are consturctors

// this is constructor 'BankAccount b = new BankAccount()'
public BankAccount() 
{
    this.accountNumber = BankAccount.nextID++;
    //
}

// and this is constructor 'BankAccount b = new BankAccount("Account", 1.0)'
public BankAccount(String accountHolder, double overdraftLimit)
{
    this.accountNumber = BankAccount.nextID++;
    //
}   

You can read more about constructors here

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

7 Comments

As an aside, nextID should be an AtomicInteger.
@RohitJain Sure they would be different. They would just start with 0, then the next is 1, 2, etc.
@RohitJain why? nextID is incremented with nextID++
@rgettman What do you think this.accountNumber = BankAccount.nextID++; assignment is doing?
@RohitJain The first one will get an account number of 0, and the post increment increments the static nextID to 1. The second one will get an account number of 1, and the post increment increments the static nextID to 2, and so on.
|
4

The problem is that what you think you've defined as constructors aren't constructors. They are just methods, and they aren't called ever. So Java inserted a default constructor, and by default it initialized your accountNumber to 0 always.

Change

public void BankAccount(){
public void BankAccount(String accountHolder, double overdraftLimit){

to

public BankAccount(){
public BankAccount(String accountHolder, double overdraftLimit){

Comments

1

Some issues:

  • Below are not the constructers.

    public void BankAccount(){
    this.accountNumber = BankAccount.nextID++;
    this.currentBalance = 0;
    this.overdraftLimit = 0;
    

    }

    public void BankAccount(String accountHolder, double overdraftLimit){
    this.accountNumber = BankAccount.nextID++;
    this.currentBalance = 0;
    this.accountHolder = accountHolder;
    this.overdraftLimit = overdraftLimit;
    

    }

For constructers please follow the below lines:

public BankAccount(){
        this.accountNumber = BankAccount.nextID++;
        this.currentBalance = 0;
        this.overdraftLimit = 0;
    }
  • the nextID should be declared as AtomicInteger:

        Integer currentInteger = atomicInteger.get();
        Integer nextInteger;
        do {
            currentInteger = atomicInteger.get();
            nextInteger = currentInteger + 1;
        } while (atomicInteger.compareAndSet(currentInteger, nextInteger));
    
  • it's good to initialize the static variables like below (not mandatory):

    private static int nextID;
    
    static{
        nextID=0;
    }
    
  • It's good to increment first than assign rather than reverse (not mandatory).

        this.accountNumber = ++BankAccount.nextID;
    

5 Comments

the static variable is automatically initialized to 0 (and it's also possible to do the initialization in the same line, it doesn't need to be in a static block but that's a matter of opinion I guess). And incrementing first and then assigning or in the other order depends on the situation.
@SimonAndréForsberg very true. Thats why i mentioned it's not mandatory. But it is a good practice, sometimes you can have objects apartfrom int, at that point you need static block. And for pre and post increment, if you use pre increment you will get accountnumber as 0 which i felt is not good than having 1. So it's non mandatory.
You can initialize non-primitive objects on the same line also. private static MyClass something = new MyClass("a whole lot of parameters");
@SimonAndréForsberg if the object is hashmap and it initializes from file reading, so there are lot many places you need static block. So it's a good practice thats all. Else your point is correct.
Sure, for situations where you actually need to do more than one statement with the variable, a static block is very useful. (Although many fantastic things can be done with the lovely Builder pattern)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.