Hi,
Not very sure if I had understood the concept of static variable /methods but the below piece of code is causing some trouble.
public class Foo {
int i = 5;
static int k = 2;
public static void main(
String[] args) {
int j = i; // Wrong because i is an instance variable
m1(); // Wrong because m1() is an instance method
}
public void m1() {
// Correct since instance and static variables and methods
// can be used in an instance method
i = i + k + m2(i, k);
}
public static int m2(int i, int j) { // -> A
return (int)(Math.pow(i, j)); // -> B
}
}
As instance variables and methods can only be used from instance methods, not from static methods, then why at // A and //B I am not getting the error
m2 is an static methods and its using i, which is an instance variable.
Please clarify this doubt.
Thanks,
Faraz