i’m currently just fooling around with different classes to test how they work together, but im getting an error message in NetBeans that i cant solve. Here’s my code:
class first_class.java
public class first_class {
private second_class state;
int test_tal=2;
public void test (int n) {
if (n>2) {
System.out.println("HELLO");
}
else {
System.out.println("GOODBYE");
}
}
public static void main(String[] args) {
state.john();
TestingFunStuff.test(2);
}
}
class second_class
public class second_class {
first_class state;
public int john () {
if (state.test_tal==2) {
return 4;
}
else {
return 5;
}
}
}
Apparently i can’t run the method “john” in my main class, because “non static variable state cannot be referenced from a static context” and the method “test” because “non static method test(int) cannot be referenced from a static context”.
What does this mean exactly?
Screenshot of the error shown in netbeans: http://imageshack.us/photo/my-images/26/funstufffirstclassnetbe.png/
It means
statemust be declared as a static member if you’re going to use it from a static method, or you need an instance offirst_classfrom which you can access a non-static member. In the latter case, you’ll need to provide a getter method (or make it public, but ew).Also, you don’t instantiate an instance of
second_class, so after it compiles, you’ll get aNullPointerException: static or not, there needs to be an instance to access an instance method.I might recommend following Java naming conventions, use
camelCaseinstead ofunder_scores, and start class names with upper-case letters.