Adib Adib -4 years ago 81
Java Question

Invalid Type Error Inside an 'If' statement

Hello friends for an OOP exercise, I created a parent class with the following function

public class Account {
private double dblBalance = 0;
private double dblAmmount = 0;

Account(double dblBalance, double dblAmmount){
this.dblBalance = dblBalance;
this.dblAmmount = dblAmmount;
}

double getBalance(){
return this.dblBalance;
}


Then I have a subclass which extends the parent class and looks like this

public class MinBalance extends Account {

double dblFees = 0; // A variable unique to this account

double bal = super.getBalance();



if( bal < 500.0){ // Error: Illegal start of type, cannot find class bal
// dblLevy gets changed
}

MinBalance(double dblBalance, double dblAmmount) { //Subclass MinBalance's constructor
super(dblBalance, dblAmmount);
}

}


The if statement is giving me the following error "Illegal start of type, cannot find class bal"
I'm having a hard time understanding what is going wrong in this but I feel like its a simple mistake with the If statement...
In fact, if I replaced bal with anything else (Even a real number / integer) I still receive the same error. What is going on and how do I prevent it?

Answer Source

You must put the if statement in a method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download