I am new with java.
I have this error message “non-static variable cannot be referenced from a static context“.
I’ve read some answers here in S.O but I could not adapt the solutions to my problem.
This is the code:
package test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class test{
public class Pass {
private int identity;
private ArrayList<Date> logged_in;
private ArrayList<Date> logged_out;
public Pass(int identity){
this.identity = identity;
this.logged_in = new ArrayList<Date>();
this.logged_out = new ArrayList<Date>();
}
}
public class Officer {
private Pass pass;
public Officer(Pass pass){ this.pass = pass; }
}
public static void main(String[] args) throws ParseException {
Officer officer1 = new Officer(new Pass(1111));// PROBLEM IN THIS LINE
}
}
Thanks in advance for your help.
You’ll want to take the Officer and Pass classes outside of the “test” class, so that they live side by side with the “test” class and not within it.
UPDATE: As other answers suggest, only one public class per file. I’ve updated the code to reflect that. Also, classes are generally placed in their own file and do not live all together.