0

I'm trying to make a method which gets the text from a txtfile into a String. The method is working when I'm calling it from the same class that it's in (That class is an activity). But when I'm calling it from another class (an ordinary class) it needs to be static. And I don't know much about static, but the method is not working when it's static. As I need to call the method from another class, I either need the method to be non-static or fix the error when it's static. Here is the method:

public static String loadData(String inFile) {
    String str = "";
    try{
        StringBuilder buf=new StringBuilder();
        InputStream json= getAssets().open(inFile);
        BufferedReader in=
                new BufferedReader(new InputStreamReader(json, "UTF-8"));
        while ((str=in.readLine()) != null) {
            buf.append(str);
        }
        in.close();
        return buf.toString();
    } catch (Exception e) {
        Log.e("er0r", e.toString());
    }
    return str;
}

The method is called from a class Solve.java:

public static void permutateYellowEdges(){
    try {
        Rotations.rotateSequence(MainActivity.loadData("tables\\topLayer\\edgePerm.rt"), States.getYellowEdgeState());
    }catch(Exception e){
        e.printStackTrace();
    }
}

The error I get is a red line under "getAssets" which says "Non-static method getAssets can not be referenced from a static context". How could I fix this?

John
  • 23
  • 7
  • Possible duplicate of [What is the reason behind "non-static method cannot be referenced from a static context"?](http://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static) – Timothy Truckle Apr 18 '17 at 20:27
  • Certain duplicate of this very funny 'i wont listen thread': http://stackoverflow.com/questions/43476217 – greenapps Apr 18 '17 at 21:46

4 Answers4

1

You would need to make the getAssets() static, or call it using a static reference. Currently it is defined as a non-static method, and you need to have your methods be either static or non-static. Otherwise, you need to create the object for the class that getAssets sits in.

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
1

Non-static method could be only invoked from created instance of the class. Static method doesn't require this. So when you call non-static from static, non-static method is not invoked necessarily on instance (the instance may not be created). Thus your code is not compiled.

Vasiliy Vlasov
  • 3,316
  • 3
  • 17
  • 20
0

You need to create an object of the class the getAssets() method is in. Since your getAssets() method is not declared as Static, an object of the class it is housed in needs to be created before it is called. If it was a static method it could be called without initiating an object but still with reference to what class it belongs to. For example:

public class classWhereGetAssetsIsIn {
    //More code here. Constructor is not necessary.
    typeMethodReturns getAssets() {
        //Code here.
    }
}

To call the above method inside any class or even the class itself if you are calling it from a static context (f.e. public static void main(String[] args) {})

classWhereGetAssetsIsIn instance = new classWhereGetAssetsIsIn();
InputStream json= instance.getAssets().open(inFile);

The reason the above code works is because the method getAssets() is created within the object that is constructed from the constructor of the class. If no constructor has been declared, the java compiler creates a default empty one as shown above. Keep in mind that if you have set your own constructor you need to construct an object using your own constructor since the java compiler creates the empty one only when the class has none (Meaning even if you have one constructor that takes arguments in, the java compiler won't create the empty one). The above code can shorthanded-ly be written as follows:

InputStream json= (new classWhereGetAssetsIsIn).getAssets().open(inFile);

By declaring the method as static you are binding it to the class and not to the objects it creates, meaning that you can call it by writing:

InputStream json = classWhereGetAssetsIsIn.getAssets().open(inFile);

Or if you are working inside the public static void main(String[] args){} of the same class getAssets() is housed in, you can call it as your original code shows:

InputStream json = getAssets().open(inFile);

In order to make your getAssets() function static, you simply add the "static" keyword in front like you do with public static void main(String[] args){}:

public class classWhereGetAssetsIsIn {
    //More code here. Constructor is not necessary.
    static typeMethodReturns getAssets() {
        //Code here.
    }
}
0

Call it from context like

InputStream jsonStream = context.getAssets().open(InFilename);

where

    static Context context;
Josef Vancura
  • 1,053
  • 10
  • 18