Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 720529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:46:07+00:00 2026-05-14T05:46:07+00:00

$ javac TestFilter.java TestFilter.java:19: non-static variable this cannot be referenced from a static context

  • 0
$ javac TestFilter.java 
TestFilter.java:19: non-static variable this cannot be referenced from a static context
        for(File f : file.listFiles(this.filterFiles)){
                                    ^
1 error
$ sed -i 's@this@TestFilter@g' TestFilter.java 
$ javac TestFilter.java 
$ java TestFilter
file1
file2
file3

TestFilter.java

import java.io.*;
import java.util.*;

public class TestFilter {
    private static final FileFilter filterFiles;

    // STATIC!
    static{
        filterFiles = new FileFilter() {
            // Not Static below. When static, an error:
            // "accept(java.io.File) in  cannot implement 
            // accept(java.io.File) in java.io.FileFilter; 
            // overriding method is static"
            //
            // I tried to solve by the change the problem at the bottom.

            public boolean accept(File file) {
                return file.isFile();
            }
        };
    }

   // STATIC!
    public static void main(String[] args){
        HashSet<File> files = new HashSet<File>();
        File file = new File(".");

            // IT DID NOT WORK WITH "This" but with "TestFilter".
            // Why do I get the error with "This" but not with "TestFilter"?

        for(File f : file.listFiles(TestFilter.filterFiles)){
            System.out.println(f.getName());
            files.add(f);
        }
    }
}

Update: define “current object”

Constructor created, object created but the this does not refer to the current object “test”. It works when I change this to “test” but it does not work with “this”. Why?

$ javac TestFilter.java 
TestFilter.java:28: non-static variable this cannot be referenced from a static context
        for(File f : this.getFiles()){
                     ^
1 error
$ cat TestFilter.java 
import java.io.*;
import java.util.*;

public class TestFilter {

    private static final FileFilter filterFiles;
    private HashSet<File> files;

    static{
        filterFiles = new FileFilter() {
            public boolean accept(File file) {
                return file.isFile();
            }
        };
    }

    TestFilter(){
        files = new HashSet<File>();
        File file = new File(".");

        for(File f : file.listFiles(filterFiles)){
            files.add(f);
        }
    }

    public static void main(String[] args){

        // CONSTRUCTOR with no pars invoked and object "test" created here!

        TestFilter test = new TestFilter();

        // Why does it not work with "this"? 
        // "test" is surely the current object.

        for(File f : this.getFiles()){
            System.out.println(f.getName());    
        }
    }

    public HashSet<File> getFiles() { return files; }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:46:08+00:00Added an answer on May 14, 2026 at 5:46 am

    Why do I get the error with “this” but not with “TestFilter”?

    • this is used to refer to “instance” attributes or method ( among others ). Instance means a new object exist and each object ( instance ) have a copy of the given attribute.

    • The class name ( in your case TestFilter ) is used to refer to “class” attributes or methods ( those who do not require an instance to extist.

    So, in your first line you’re declaring filterFiles as a class attribute ( you don’t require an instance for that.

    See:

    private static final FileFilter filterFiles;
    

    This means, you declare class attribute named: filterFiles of type FileFilter which is private and whose reference can’t be changed ( because it is final).

    Since it is a class attribute you may access it in the main method ( which is a class level method ). This both will work:

    for(File f : file.listFiles(TestFilter.filterFiles)){
    

    and

    for(File f : file.listFiles(filterFiles)){
    

    But

    for(File f : file.listFiles(this.filterFiles)){
    

    Won’t, because this refers to the current instance, but since you’re in a class level method ( main ) there is no instance, so, there is no this or in compiler words: non-static variable this cannot be referenced from a static context

    Instance attributes are unique per instance. Class level attribute are unique per class.

    Consider the following class:

    import static java.lang.System.out;
    class Employee  {
         // class level counter. It exist regardless of the instances created.
         public static int employeeCount = 0;
         // instance attribute. Each one is different from others instances
         private String employeeName;
    
         // Class level method, can be invoked without instance.
         public static Employee createEmployee( String withName ) {
    
             Employee e = new Employee();
             // Set the name to the instance
             e.employeeName = withName;
             // Increments the class counter
             Employee.employeeCount++;
             return e;
         }
         // Object constructor.
         public Employee() {
              out.println("Constructor invoked!!! A new object has born, yeah!");
         }
         // Instance method "toString()"
         public String toString() {
             // Uses "this" to refer instance level method
             return this.emploeeName;
         }
    
         // Test
         public static void main( String [] args ) {
    
              // The counter already exist
              out.printf("Employees created %d%n", Employee.employeeCount );
              // Create employee a
              Employee a = Employee.createEmployee("Oscar");
              // Its name now exists 
              out.printf("Employee name: %s %nEmployees created %d%n",
                          a.employeeName,  Employee.employeeCount );
              // Create employee b with a new name 
              Employee b = Employee.createEmployee("HH");
              out.printf("Employee name: %s %nEmployees created %d%n", 
                          b.employeeName,  Employee.employeeCount );
              // Now both employees exist, each one with a name 
              out.printf("a=%s, b=%s%n, a, b );// invoke toString method which in turn uses "this"
    
         }
    }
    

    I hope this sample make everything clear.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 450k
  • Answers 450k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, what you can try is include some javascript after… May 15, 2026 at 8:39 pm
  • Editorial Team
    Editorial Team added an answer Try this: my $hex = "DEADBABEDEADBEEF"; my @a = map… May 15, 2026 at 8:39 pm
  • Editorial Team
    Editorial Team added an answer The v2 API had the GPolyline.getBounds() method to do exactly… May 15, 2026 at 8:39 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.