0

I have a line in my homework that I'm having a hard time with.

You are supposed to provide methods to withdraw and deposit a given amount of money from an account and a transfer method that transfers given amount of money from one account to another. The transfer method can be called by using the class name.

I don't quite understand what the last sentence means. Here is what I have;

public class CbtBank {
    private double balance;
    private String firstName, lastName;
    private int accountNumber;

    public CbtBank(String firstName, String lastName, int accountNumber, double balance) {
        this.balance = balance;
        this.firstName = firstName;
        this.lastName = lastName;
        this.accountNumber = accountNumber;
    }

    public void transfer(CbtBank from, CbtBank to, double amount){
        to.balance += amount;
        from.balance -= amount;
    }
}

And my main.java;

public static void main(String[] args) {
    CbtBank person1 = new CbtBank("can", "berk", 3123, 100.0);
    CbtBank person2 = new CbtBank("can2", "berk2", 3124, 200.0);
    CbtBank.transfer(person1, person2, 50.0);
}

I can't get it to work this way but I'm not even sure if this is meant. Any ideas?

2
  • 1
    Look at the concept of static in Java. Commented Nov 4, 2013 at 16:42
  • Thanks a lot, I think I've missed that lecture, I had no idea. Commented Nov 4, 2013 at 16:44

5 Answers 5

1

In the doc

In this section, we discuss the use of the static keyword to create fields and methods that belong to the class, rather than to an instance of the class.

Then your requirement is method which belongs to a class not instance.

Static Methods can be accessible by its name, so make CbtBank#transfer a staticmethod.

class CbtBank
{
     public static void transfer(CbtBank to,CbtBank from,double value)
     {
         to.balance += amount;
        from.balance -= amount;
     }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I was missing the "static" keyword. So the correct version is;

    public static void transfer(CbtBank from, CbtBank to, double amount){

    to.balance += amount;
    from.balance -= amount;

}

Thanks for the answers.

Comments

0

Just make the method static. This way, you can call it without an actual object but by using the class itself.

public static void transfer(CbtBank from, CbtBank to, double amount){
    to.balance += amount;
    from.balance -= amount;
}

Comments

0

static methods can be called by using the classname. You may have to turn your transfer method to static understanding its implications. Once you do that then you call your transfer method as mentioned here:

CbtBank.transfer(person1, person2, 50.0);

Comments

0

It's related to the concept of static. This defines either a variable or a method that's not related to a concrete instance of a given type, but to the class itself.

Here, a transference is not something particular of an account, plus it's logic and your CbtBank object is what could be called a Plain Old Java Object. The logic should be elsewhere, but, in this case, it is located in the class.

Think about it as this: An account shouldn't be, itself, in charge of a transaction. Account A shouldn't tamper with account B's balance because that's account B's private information.

If it was me, I'd ask the method to be in another class, but stick to your assignment. Declare the method as static and make a static call:

Account.transfer(CbtBank from, CbtBank to, double amount)

Comments

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.