abarnybox
abarnybox

Reputation: 139

Accessing other method variables in java?

public class Document1 {
   public static void main(String[] args) {
     String i = "A";
     eg();
   }

   public static void eg(){
      System.out.println("    " + i);
   }
}

I don't understand why this doesn't work.

Upvotes: 1

Views: 228

Answers (8)

DT7
DT7

Reputation: 1609

You should pass i as an argument to method eg().

In your code, i is a local variable in main method. Therefore, it cannot be accessed by your eg() method.

Try:

public static void main(String[] args) {
     String i = "A";
     eg(i);
   }

Upvotes: 1

Fernando Moraes
Fernando Moraes

Reputation: 21

Because i only exists in main scope. you have to pass i inside eg function, like this eg(String param).

Upvotes: 2

Bohemian
Bohemian

Reputation: 424983

It's all to do with variable scope - "scope" meaning where the variable is accessible.

In your code, i is a "local variable", that has scope only within the method it is declared (and in fact only after it's declared). That's why the other method can't "see" it.

Parameters to methods are considered local variables too.

The next level up is an instance variable, whose scope is all methods, but only within the context of an instance - each instance gets its own copy.

After that you've got static variables, whose scope is every instance - there's only one copy per class in which it's defined.


To "fix" your code you can:

  • pass the variable as a parameter to the eg() method
  • create a static variable, which would be declared outside the method(s)
  • because your methods are static an instance variable wouldn't help, but would work if you made your methods instance methods (non-static) and created an instance of your class

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

scope

You get an compiler error in the below method.

 public static void eg(){
      System.out.println("    " + i);
   }

Because i is local variable to main method. You cannot access in other methods.

What you can do is, pass that variable to where you want.

 public static void main(String[] args) {
     String i = "A";
     eg(i);
   }

   public static void eg(String param){
      System.out.println("    " + param);
   }

Upvotes: 4

christopher
christopher

Reputation: 27336

This is what is known as an issue of scope. scope is the area of code where a variable is visible. For example:

for(int x = 0; x < 10; x++)
{
     // x can only be used inside this loop.
}

Another example is if you declare a local variable inside a method:

private void doSomething()
{
     String value = "this can only be used inside doSomething()";
}

Here is a pretty helpful link explaining the concept.

For your issue, you simply declared a variable inside the method. You should make it a parameterized method:

public void eg(String i)
{
    // Your code as normal.
}

Upvotes: 1

Andrew Schuster
Andrew Schuster

Reputation: 3229

It's a matter of scope. Your function eg() cannot "see" the variable i because it isn't within its view. If, however, you did as follows:

public class Document1 {
    String i = "A";

    public static void main(String[] args) {

        eg();
    }

    public static void eg(){
        System.out.println("    " + i);
    }
}

Then it would work because now the variables scope is the whole class file and not just the main method.

Upvotes: 0

Kevin DiTraglia
Kevin DiTraglia

Reputation: 26058

You can't access String i from your function eg() because it was declared in the scope of main. You can either declare i in a global scope by placing String i above your main function. Or pass it as parameter to your function such as:

public static void eg(String i){

and change the function call to:

eg(i);

Upvotes: 0

Rahul
Rahul

Reputation: 45060

Because eg() method is not aware of the variable i. Its within the scope of main() method only. You need to either pass it to the eg() method or make it an instance variable (in this case, a static variable).

public static void main(String[] args) {
   String i = "A";
   eg(i);
}

public static void eg(String i){
   System.out.println("    " + i);
}

Upvotes: 0

Related Questions