Email address:
Password:
SCJP/OCPJP 1.6 certification
Practice Exams and Training

capture(); is called outside any method. It is static.

then what is rule of calling methods outside other methods. like for instance methods

The only thing you have to understand is the whole realm of 'static' is shared thing among objects. When you have a variable called x which is static, you can always access it from different objects of the enclosing class, and it will always refer the same variable. For example, consider this:


class Encloser {

   static int x = 2;

}


Encloser e1 = new Encloser();
Encloser e2 = new Encloser();
Encloser e3 = new Encloser();


Now if you access x from any of the above three objects, it would always refer the same:


Encloser.x = 4;
e1.x = 5;
e2.x = 6;


All of the above three references are equivalent, and refer the same variable. After running the above code, if you print Encloser.x, e1.x, e2.x, or e3.x, it always prints '6' - because there's only one variable, and it has gotten the last value it set.


If 'x' was NOT a static variable, it's works the opposite way. In that case, e1.x and e2.x and e3.x are three different variables, and they can contain different values at any given moment.


This is the same when it comes to methods.


Now, when it comes to the question of which can be referred in which context, consider this:


Can you refer a static variable from a static context? Sure! Since the variable is static, there's only one copy of the variable, and therefore JVM knows which variable to access

Can you refer a static variable from a non-static method? Sure, for the same reason as above.

Can you refer a non-static variable from a non-static context? Sure! You are trying to access it in a non-static context means, you are already on an object of that class, so the JVM knows that when you say just 'x', it means the copy of 'x' of the object you are currently on.

Can you refer a non-static variable from a static context? No! You are on a static context means you are currently not running on an object. At that point, unless you point out which object, JVM has no way to know which object's x you are trying to access


Now, to map this to our code in the question, let's isolate the problem:


public class ExamLabBoard {

  static int cam=capture();

  public static int capture(){
    return door;
  }

}


The question is whether you can do the above thing. To figure that out, be the compiler! Being the compiler, let's arrive at the line where it initializes the variable cam. You know, when it comes to initializing variables, there are two initializers do the job - static initializer and non-static initializer. Now, which initializer would run this line? Static initializer would! Why? Because cam is a static variable, and therefore it's static initializer's job to initialize it. Now at the time of this initialization happens, it needs to execute the method capture() to obtain the value to assign to cam. Given that capture() is a static method, the static initializer knows there's only one copy of the method, so it can simply access it!


In contrast, can you apply this analogy to what it would be if capture() was a non-static method?




I guess if capture would NONstatic then it will not compile

because at compile time compiler will not Know which capture() to call.


BUT


class A{
  static int  cam=new A().capture();
  int capture() {
    return 1;
  }
}


then will it compile??

Correct - by doing that, you are pointing which object to look for.

ExamLab © 2008 - 2024