It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock Exam Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock Exam Doubt" Watch "Mock Exam Doubt" New topic
Author

Mock Exam Doubt

kamesh vadarevu
Greenhorn

Joined: Oct 17, 2000
Posts: 20
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
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
Thank you very much Aruna.I appreciate for clearing my doubt.
kamesh
kamesh vadarevu
Greenhorn

Joined: Oct 17, 2000
Posts: 20
Thank you very much Aruna.I appreciate for clearing my doubt.
kamesh
kamesh vadarevu
Greenhorn

Joined: Oct 17, 2000
Posts: 20
Thank you very much Aruna.I appreciate for clearing my doubt.
kamesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mock Exam Doubt
 
Similar Threads
Question from Marcus Green
passing by value
Variable change
doubt from the question!
Marcus Green - Qn : 23 - Doubt