So, Im trying to learn JAVA by myself and have come across the infamous "non-static variable this cannot be referenced from a static context" error.
I know its a repeat for you guys, but I dont understand why its happening...
public class JavaApplication1 {
public static void main(String[] args) {
/** Error comes from line below */
Anagram Pay = new Anagram(3,"PAYPAL");
System.out.println(Pay.horzArray);
}
public class Anagram{
private int vertArray;
private int horzArray;
private String starterWord;
private int wordLength;
private int diagonalMax;
public Anagram(int verticalArray, String inputWord){
vertArray = verticalArray;
starterWord = inputWord;
wordLength = starterWord.length();
if (vertArray > 2)
diagonalMax = vertArray - 2;
else
diagonalMax = 0;
horzArray = wordReducer(wordLength);
}
public int wordReducer(int word){
int step = 1;
int horzValue = 0;
while (word > 0)
if (step == 1) {
word = word - vertArray;
step = step + 1;
horzValue = horzValue + 1;
} else {
word = word - diagonalMax;
step = 1;
horzValue = horzValue + diagonalMax;
}
return horzValue;
}
}
};
(P.S If anyone is curious - Im in the process of answering this: https://leetcode.com/problems/zigzag-conversion/description/ )