Panos Kalatzantonakis
Panos Kalatzantonakis

Reputation: 12673

Non static variables from a static context

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.

Upvotes: 1

Views: 144

Answers (2)

Vishal
Vishal

Reputation: 3279

Only problem with your code is the user of Inner classes. As you are a beginner, so I assume you do not want to dig into inner classes.

So you must define 3 classes independent to each other.

Class Pass is public class...

Class Officer is second public class

class Test is another public to test the functionality.

so

Pass{
}

Officer{

}

Test{

}

At the beginning we confuse ourselves, and try to write everything inside braces. But it does not hold true always. But it is good practice to declare one class in a individual file. So in your example...

Pass.java should contain Pass class...

Pass {

}

Officer.java should contain Officer class...

Officer {

}

Same holds true for Test class as well...

Beside you can choose to study from some good book, which would help you build your fundamentals first.

Upvotes: 1

David Gutierrez
David Gutierrez

Reputation: 383

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.

package test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;


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>();
    }    
}

class Officer {    
    private Pass pass;
    public Officer(Pass pass){ this.pass = pass; }    
}    

public class test{
    public static void main(String[] args) throws ParseException {  
        Officer officer1 = new Officer(new Pass(1111));// PROBLEM IN THIS LINE 
    }
}

Upvotes: 7

Related Questions