Hi, I'm reading this book about Java right now and in it was this example so I wrote it off and tested it...

I get the error "Exception in thread "main" java.lang.NoSuchMethodError: main" and from what I've understood it's because I am missing a main class... Here's the deal though:

1) Have I missed something or is this just an incomplete part or why didn't the author include any main class?
2) As a newbie to this, I feel very confused since several other of his examples are like this. What would the best thing to do in this situation? Should I not continue to write these, what would be the best to fix this etc...?

public class Bankaccount {
	private int accntNbr; //kontonummer
	private int balance; //saldo



	/** skapar ett bankkonto med numret accntNbr och saldot noll */
	public Bankaccount (int accntNbr) {
		this.accntNbr = accntNbr;
		this.balance = 0;
	}

	/** Tar reda på kontonummret */
	public int getAccntNbr() {
		return accntNbr;
	}

	/** saldo */
	public int getBalance (){
		return balance;
	}
	/** Sätter in amount kronor på konto */
	public void deposit(int amount){
		balance = balance + amount;
	}
	/** tar ut amount från kontot */
	public void withdraw (int amount) {
		if (amount <=balance){
			balance = balance - amount;
		} else {
			System.out.println("Du försökte ta ut" + amount);
			System.out.println("men du har bara" + balance);
			System.out.println("på ditt konto");
		}
	}

}

you do not have create main class for your program. make another class which have main() function and create object of that class. I Hope this will work.

As stokes1900 is saying, you need a main method, not a main class. You can put a main method in your Bankaccount class, or better, you can write another class to test the Bankaccount that has a main. Did the author of the book you're reading provide something like BankAccountTester class?

If you want a quick test on the class you are working on, add a main method in the class (could be at the bottom of your class implementation). A sample main() definition is below.

public class Bankaccount {
  // your class variables declaration

  // any other methods...


  // add main() method to the class, so you can run it after you compiled the class
  // >javac Bankaccount.java
  // >java Bankaccount
  public static void main(String args[]) {
    Bankaccount ba = new Bankaccount(1234);  // the argument must match its constructor

    // test getAccntNbr() by displaying it
    // expect 1234 to be display on the console
    System.out.println(ba.getAccntNbr());
    // test getBalance() by displaying it
    // expect 0 to be display on the console
    System.out.println(ba.getBalance());
    // test deposit() by displaying it
    // expect balance to be 50 to be display on the console
    ba.deposit(50);
    System.out.println(ba.getBalance());
    // test invalid withdraw() by displaying it
    // ** note ** notice that there is no white space between the amount (from your code)
    // expect 3 lines below are displayed on the console
    // Du försökte ta ut100
    // men du har bara50
    // på ditt konto
    ba.withdraw(100);
    // test valid withdraw() by displaying it
    // expect balance to be 10 to be display on the console
    ba.withdraw(40);
    System.out.println(ba.getBalance());
  }
}

Edited 6 Years Ago by Taywin: n/a

you need a main method, not a main class. this main method could either be in your Bankaccount class which you displayed or in another class (which creates a Bankaccount object).

This question has already been answered. Start a new discussion instead.