0

I would like to know if it is possible to access a variable declared in another file. For example:

httpPostFileUpload(client,
                   "/data/data/fshizzle.com/files/image.jpg",
                   "http://10.0.2.2/upload.php", "uploaded",
                   s.getSelectedItem().toString());                     

Here, I'd like to replace http://10.0.2.2/upload.php with a URL stored in a variable, but with the variable declared in another file. How do I do this in Java?

1 Answer 1

4

You can declare in another java file a public static variable which can then be accessed every where else.

For example,

Class1.java

package com.my.app;
public class Class1 {
    public static String URL = "http://10.0.2.2/upload.php";
}

Class2.java

package com.my.app;
public class Class2 {
    public void Function(){
        httpPostFileUpload(client, "/data/data/fshizzle.com/files/image.jpg", 
                       Class1.URL, "uploaded", s.getSelectedItem().toString());
    }
}

Class2 can see Class1 because both are in the same package (if they weren't, a simple import Class1; would fix this)

The static keyword means you can use the variable even without having access to an object of the specified class.

Finally, the public keyword allows you to access the variable from outside the class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.