Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make this static??

Status
Not open for further replies.

DonCL

Programmer
Jul 16, 2001
43
US
When i run the following, i get this error message:

FileList.java:21: non-static method searchDir(java.io.File,java.lang.String) can
not be referenced from a static context
matching = searchDir(curDir, search);
^

Thanks for any help. Here is the code:
Code:
import java.io.File;

import java.util.*;

public class FileList {
   public static void main(String args[]) {
      File curDir = new File("C:/");
      byte b[] = new byte[100];
      String search;
      String s[];
      Vector matching;

      System.out.print("Please enter your search criteria: ");
      
      // get your input
      System.in.read(b);

      // decode the byte array 
      search = new String(b);

    matching = searchDir(curDir, search);
      
      // to get the values in the Vector
      s = new String[matching.size()];

      System.out.println("Matching results:");
      for(int i=0; i < matching.size(); i++)   {
         s[i] = (String)matching.elementAt(i);
         System.out.println(s[i]);
         
         // you don't really need the s[], you could also do this
         // System.out.println(matching.elementAt(i));
      }
        
   }
   

   public Vector searchDir(File curDir, String searchText){
      Vector matching = new Vector();   
   
      File[] allFiles = curDir.listFiles();   
   
      for(int c=0; c < allFiles.length; c++)   {
         if (allFiles[c].isFile())   {
         
            // this will match any file that contains the searchText
            if (allFiles[c].getName().indexOf(searchText) >= 0)
               matching.addElement(allFiles[c].getName());  
      
         }
      }
      return matching;
   }
}
 
You are calling searchDir from your main method, which is a static method. As a rule of thumb, you can access static things from non-static things, but you can't access non-static things from static things. Read up on static methods to get a better understanding of this.

The more traditional way of doing this is to put all the code that's in main into a constructor for your class (or into a method called from your constructor), then just use main to instantiate another instance of your entire class, like this:

public class FileList
{
public FileList()
{
...all your code that's in main right now
}

public Vector searchDir(File curDir, String searchText)
{
}

public static void main(String[] args)
{
new FileList();
}
}
 
There are two things that you can do.

1. You can make the method searchDir a static method.
2. You must instaniate a FileList object in your main method and use the searchDir method via that object. So that would mean your code would look something like this:

public static void main(String args[]) {
File curDir = new File(&quot;C:/&quot;);
byte b[] = new byte[100];
String search;
String s[];
Vector matching;

System.out.print(&quot;Please enter your search criteria: &quot;);

// get your input
System.in.read(b);

// decode the byte array
search = new String(b);

FileList fl = new FileList();
matching = fl.searchDir(curDir, search);
.
.
.

-gc
&quot;I don't look busy because I did it right the first time.&quot;
 
Thanks, I will try these methods.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top