user3411010
user3411010

Reputation: 55

Cannot declare private variables in constructor?

I'm programming in Java and I'm having trouble getting the following code to compile. The error (illegal start of expression) shows up at

private final int YA;

However, when I remove the private modifier on all the variables, it compiles fine.

I haven't been able to find a solution online, and most of my searches end up going toward questions about making constructors private (which I'm not trying to do). What really gets me is I've written another class that follows the same format, private final variables in the constructor, and it compiles without a problem.

I'm new to Java, so I could be missing something really boneheaded here. If so, please be gentle.

Cheers and thanks.

public class IndividualTaxCalculator {

    IndividualTaxCalculator(int inYearAssessment) {

        private final int YA;
        YA = inYearAssessment;

        switch (YA) {

            case 2013:

                private float netEmploymentIncome;      // BTC cell H16
                private float totalIncome;              // BTC cell H27
                private float assessableIncome;         // BTC cell H31
                private float chargeableIncome;         // BTC cell H49
                private float taxPayableOnChargeable;   // BTC cell H51
                private float incomeTaxRebate;          // BTC cell H53
                private float taxPayableAfterRebate;    // BTC cell H55
                private float parenthoodTaxRebate;      // BTC cell H57
                private float netTaxPayable;            // BTC cell H59

                break;

        }

    }

}

Upvotes: 1

Views: 1886

Answers (4)

TKV
TKV

Reputation: 2653

no, you cannot.. you can declare those private variables outside of constructor and you can assign values to them.

Upvotes: 0

CWood
CWood

Reputation: 23

When you're making a class in Java, its member variables have to be declared inside the class, but outside of any methods. Member variables are the ones that you want to access in any method of your class, and are the ones that need access modifiers, such as private.

Therefore, what I would suggest for your code, would be to move all of your variables to being declared outside the constructor, and initialize them in the constructor if YA==2013. So, to refactor your code a bit:

public class IndividualTaxCalculator {
    private float netEmploymentIncome;      // BTC cell H16
    private float totalIncome;              // BTC cell H27
    private float assessableIncome;         // BTC cell H31
    private float chargeableIncome;         // BTC cell H49
    private float taxPayableOnChargeable;   // BTC cell H51
    private float incomeTaxRebate;          // BTC cell H53
    private float taxPayableAfterRebate;    // BTC cell H55
    private float parenthoodTaxRebate;      // BTC cell H57
    private float netTaxPayable;            // BTC cell H59

    IndividualTaxCalculator(int inYearAssessment) {
        switch (inYearAssessment) {
            case 2013:
                // Initialize the variables here

                break;
        }
    }
}

Upvotes: 0

Safrain
Safrain

Reputation: 294

You can only use private/public/protected on member like this

public class IndividualTaxCalculator {
    private int foo;
    private int bar;
    ....
}

While you declare a variable in a method, the variable can only be accessed in the method.

Upvotes: 0

user1907906
user1907906

Reputation:

Move the member outside of the constructor:

private final int YA;

IndividualTaxCalculator(int inYearAssessment) {
  YA = inYearAssessment;
  // ..
}

And please don't name a non-static member in upper-case letters. Those are used for constants.

Use

private final int ya;

instead.

Upvotes: 4

Related Questions