• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock Exam Doubt

 
kamesh vadarevu
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Aruna.I appreciate for clearing my doubt.
kamesh
 
kamesh vadarevu
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Aruna.I appreciate for clearing my doubt.
kamesh
 
kamesh vadarevu
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Aruna.I appreciate for clearing my doubt.
kamesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic