Mike
Mike

Reputation: 2386

Problems when calling strings from other methods

I have a declared a String with the class as follows:

public static String nixprocessvalue;

I assign some data to the string with this method:

public static void GetStatsNix(String operation)
{

    String result = null;
    try {
        Runtime r = Runtime.getRuntime();                    
        Process p = r.exec("/bin/hostname");
        BufferedReader in =
        new BufferedReader(new InputStreamReader(p.getInputStream()));
        String inputLine;
        inputLine = in.readLine();

    }

Upvotes: 0

Views: 86

Answers (2)

AlexS
AlexS

Reputation: 5345

} catch (IOException e) {
    System.out.println(e);
}    public static void GetStatsNix(String operation)
{

This last line looks very suspicious to me, because your actual function isn't closed yet, but another method definition is started (GetStatsNix(String operation)).

Are you sure, that you don't have some wrong placed brackets?

Upvotes: 0

Alexander Rühl
Alexander Rühl

Reputation: 6939

For your question you should try to concentrate on the important points, in your case the changes you make to the variable in question.

Because it's not a general problem of scope - consider this simple example:

public class StaticTest {

    public static String s;

    public static void main(String[] args) {
        write();
        read();
    }

    static void write() {
        s = "Hello";
    }

    static void read() {
        System.out.println(s);
    }
}

The static variable is written and read in two different methods, which is perfectly fine.

The question is, why you declare everything static - just because you do everything from main and don't feel like making object instances is a good idea in java or has it a special reason?

Upvotes: 1

Related Questions