IPhonick 9 IPhonick 9 - 1 year ago 77
Java Question

Can't Access Instance Variable Inside Method of same Class?

I am having difficulty accessing the variable 'InvalidAlready' Inside of the method .isLatinSquare() and I have no idea why:/

Part of Code:

public class LatinSquare {
private boolean invalidAlready = false;

//Setter and Getters for invalidAlready
public void setInvalid(){
invalidAlready = true;

public boolean testInvalid(){
return invalidAlready;

//Testing method to show whether LatinSquare is valid
public boolean isLatinSquare(int[][] array) {
if (square.getInvalid() = true) {
return false;

public static void main(String[] args) {
//Main Code Here
LatinSquare square= new LatinSquare();

The main problem I am having is with the if statement inside the isLatinSquare() method. At the if statement there is an error that says:Cannot invoke testInvalid() on the primitive type boolean

In the main method I am able to use this perfectly fine for my if statements in order to test whether the InvalidAlready object is true or false.

Sorry I am somewhat new to java :/ Any help is appreciated!
Thanks in advance!

Answer Source

InvalidAlready is a primitive boolean type, so there is no getInvalid() method defined on it:

public boolean isLatinSquare(int[][] array){

     if (InvalidAlready.getInvalid() = true) // this is wrong

You could do something like this instead:

public boolean isLatinSquare(int[][] array){

     if (InvalidAlready)
        // do stuff
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download