Hi, For the following code of Marcus Mock exam 1 the out put is 10 and 40.But my doubt is why am I not getting something like "Cannot make Static Reference to Non static method " for the static j being used in the method?can someone suggest where I am missing the Concept. thanks in advance. here is the code: public class Pass{ static int j=20;// when we are using this in the amethod??? public static void main(String argv[]){ int i=10; Pass p = new Pass(); p.amethod(i); System.out.println(i); System.out.println(j); } public void amethod(int x){ x=x*2; j=j*2; } }
Aru Ven
Ranch Hand
Joined: Sep 28, 2000
Posts: 199
posted
0
Kamesh, From static methods u can refer to only static class members. But from non static methods u can refer to any type of class variables.
If static int j was just "int j" & if the method was public static void amethod(int x){ Then u will be getting a compiler error.
HTH, Aruna
kamesh vadarevu
Greenhorn
Joined: Oct 17, 2000
Posts: 20
posted
0
Thank you very much Aruna.I appreciate for clearing my doubt. kamesh
kamesh vadarevu
Greenhorn
Joined: Oct 17, 2000
Posts: 20
posted
0
Thank you very much Aruna.I appreciate for clearing my doubt. kamesh
kamesh vadarevu
Greenhorn
Joined: Oct 17, 2000
Posts: 20
posted
0
Thank you very much Aruna.I appreciate for clearing my doubt. kamesh