Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p><em>Why do I get the error with "this" but not with "TestFilter"?</em></p> </blockquote> <ul> <li><p><code>this</code> 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.</p></li> <li><p>The <code>class name</code> ( in your case <code>TestFilter</code> ) is used to refer to "class" attributes or methods ( those who do not require an instance to extist.</p></li> </ul> <p>So, in your first line you're declaring <code>filterFiles</code> as a class attribute ( you don't require an instance for that.</p> <p>See:</p> <pre><code>private static final FileFilter filterFiles; </code></pre> <p>This means, you declare <strong>class</strong> attribute named: <code>filterFiles</code> of type <code>FileFilter</code> which is <code>private</code> and whose reference can't be changed ( because it is <code>final</code>).</p> <p>Since it is a <strong>class</strong> attribute you may access it in the <code>main</code> method ( which is a class level method ). This both will work:</p> <pre><code>for(File f : file.listFiles(TestFilter.filterFiles)){ </code></pre> <p>and </p> <pre><code>for(File f : file.listFiles(filterFiles)){ </code></pre> <p>But </p> <pre><code>for(File f : file.listFiles(this.filterFiles)){ </code></pre> <p>Won't, because <code>this</code> refers to the current instance, but since you're in a class level method ( main ) there is no instance, so, there is no <code>this</code> or in compiler words: <em>non-static variable this cannot be referenced from a static context</em></p> <p>Instance attributes are unique per instance. Class level attribute are unique per class. </p> <p>Consider the following class:</p> <pre><code>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" } } </code></pre> <p>I hope this sample make everything clear.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload