Sriram C G
Sriram C G

Reputation: 931

How to declare statements using 'this' as static?

I have an activity named Gridactivity,

public String readFile(int file)
    {
        try{
        InputStream is = this.getResources().openRawResource(file);
        byte[] buffer = new byte[is.available()];
        System.out.print(is.available());
        System.out.print(is.available());

        while (is.read(buffer) != -1)   
            ;
        String text = new String(buffer);
    return(text);
        } catch (Exception e) {
            String Ex=" ";
            return(Ex);
        }
    }

followed with a file reading method-readFile() this reads the file from the raw folder and save it in a string and return it. Here now i need to make this readFile as static. But here 'this' in InputStream is = this.getResources().openRawResource(file); statement is non-static. How to make it static function so I can access this from other classes and activities?

Upvotes: 0

Views: 266

Answers (7)

Gangnus
Gangnus

Reputation: 24484

You can't do it directly, BUT: You can declare static variables and load values into them in the onCreate function. THERE you can use this.

Or make some initialization function

initAllStaticStuff(Context context){
    someStaticVariable=context.getResources...
} 

called from onCreate as

initAllStaticStuff(this).

So you can load all needed resources into static variables and use them later.

Upvotes: 0

rekire
rekire

Reputation: 47965

A simple workaround is a pass a context.

public static String readFile(Context context, int file) {
    try{
        InputStream is = context.getResources().openRawResource(file);
[...]

And here is the call of my code:

String content=GridActivity.readFile(this, 123);

Upvotes: 1

Edison
Edison

Reputation: 5971

In your case, try

public static String readFile(Context context, int file)

and use it here:

InputStream is = context.getResources().openRawResource(file);

Basically you pass the variable as your parameter.

(getResources() is a method for Context class).

Upvotes: 1

Yugandhar Babu
Yugandhar Babu

Reputation: 10349

static members and methods can be accessed mainly with Class name and also with object name.

keyword this means current object.

If you don't know about static please read information at this lnik

Upvotes: 0

Jan Zyka
Jan Zyka

Reputation: 17898

It seems you don't understand the concept of static class members.

Static class members (variables, methods) are class level i.e. not tight to instance where ordinary fields and methods are tight to instance. You can use static members from non-static context but not vice versa.

Upvotes: 0

Buhake Sindi
Buhake Sindi

Reputation: 89189

You can never use this in a static context as this points to an instance object.

One way you could achieve static method is to pass Resources as argument, as follows:

public static String readFile(Resources resources, int file) { ... }

And, eventually,

InputStream is = resources.openRawResource(file);

Upvotes: 2

yshavit
yshavit

Reputation: 43401

You can't.

An instance method is executed within the context of an instance of its class (an instance of Gridactivity, for instance). this is the keyword that refers to this context. A static method doesn't have such a context, so what would this mean?

Your two options are to make the static method non static, or to make any methods you call from a static method also static, which means whatever information you need from your this instance will have to be explicitly passed in as arguments. So, instead of:

void myInstanceMethod() {
    System.out.println(this.myInt);
}

You'd have to do something like:

void myStaticMethod(int i) {
    System.out.println(i);
}

Upvotes: 2

Related Questions