Non-static method cannot refer to static context

I would like to understand this once and for all.

With this, please excuse the bulk of the code inserted below, but I do not want to leave any details.

The only thing I changed is the loaded URL. But this does not cause an error.

I would like to call my function " readPosiitons ". A simple solution, make it static. The real solution, I'm not sure.

Please help me better understand how to resolve this error.

Thank!!

            /*
             * 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;
                }
            }
+3
source share
5 answers

The real solution? Do not add so much material to the method main(). This is for noobs.

Java - - . , GarageComm. main() , .

:

            GarageComm gc = new GarageComm();
            hashPos= gc.readPositions("holdingsBU.txt");//the error is here
+2

mindbender Java-.

A static . non static .

main -, , , .

, static static, , , . new GarageComm().readPositions(...). , , readPosition .

+2

:

public static HashMap readPositions(String destFile) {
...
}

GarageComm , Java, .

+1

. -

  new GarageComm().readPositions("holdingsBU.txt")

.

, ?

. , . :

  • , , .

  • instanciating

, ( ), . , .

static?

, , static?,

, , .

( , ) (/ , ), .


: , , .

0
source

If the method is not static, it should be called an "on" object. When a method is called from a non-static method, this object is implied - this is the object on which the first method was called. But when you call it from a static method, the object is not implied, so to call the method you must specify the object:

GarageComm gc = new GarageComm();
hashPos= gc.readPositions("holdingsBU.txt");

Since it GarageCommdoes not have its own state, there is no reason to create an object GarageComm, so it will also mark the static method, so the object is not required to call it.

0
source

All Articles