我想一勞永逸地理解這一點。非靜態方法不能從靜態上下文中引用
有了這個,請原諒大量的代碼粘貼在下面,但我不想忽略任何細節。
我改變的唯一的事情就是加載的URL。但是這不會導致錯誤。
我想打電話給我的功能「readPosiitons」。簡單的解決方案,使其靜態。真正的解決方案,我不確定。
請幫助我更好地瞭解如何以正確的方式解決此錯誤。
謝謝!
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package PandL;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Scanner;
import toolBox.Secretary;
import toolBox.Secretary.positionObj;
/**
*
* @author Jason
*
*/
public class GarageComm {
public static void main(String[] args) throws MalformedURLException, IOException{
String retStr;
String startM;
String endM;
String myURL;
String[] Split1=null;
Integer lnCount;
HashMap hashPos=new HashMap();
hashPos= readPositions("holdingsBU.txt");//the error is here
myURL="http://myUrl?s=";
URL url = new URL(myURL);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
in.close();
}
public HashMap readPositions(String destFile){
HashMap<String, Secretary.positionObj> hashPositions=new HashMap<String,positionObj>();
Secretary mySecretary=new Secretary();
try{
File F=new File(destFile);
if(F.exists()){
System.out.println("File Exists: "+F.exists());
System.out.println(destFile);
Scanner sC= new Scanner(F);
while (sC.hasNext()){
String[] Splitter1;
Secretary.positionObj position=mySecretary.new positionObj();
Splitter1=sC.nextLine().split(",");
position.positionDate=Double.parseDouble(Splitter1[0]);
position.positionTicker=(Splitter1[1]);
position.positionOpen=Double.parseDouble(Splitter1[2]);
position.positionPrice=Double.parseDouble(Splitter1[3]);
position.positionSMA=Double.parseDouble(Splitter1[4]);
position.positionUpdated=Double.parseDouble(Splitter1[5]);
position.priceUpdated=Double.parseDouble(Splitter1[6]);
position.updateDate=Double.parseDouble(Splitter1[7]);
hashPositions.put(position.positionTicker.trim(), position);
}
}else{
System.out.println("File Created: "+ F.createNewFile());
System.out.println("----No previous positions----");
}
}catch (Exception E){
System.err.println(destFile + " does not exist.");
hashPositions.put("ERROR", null);
E.printStackTrace();
}
return hashPositions;
}
}
你問*爲什麼*你必須聲明它'靜態'? – rlibby 2011-05-12 02:46:23