Amazon Interview Question
Development Support EngineersSeriously? All the answers seem to be book definitions, if one of you guys answers me that that would be a red flag to me.
I'm not saying your answers are wrong, but it doesn't prove me that you know how to code (or have coded anything at all).
Static means that you can access a method in a class, even if that class is not instantiated.
For example:
Class Animal, has static method getFur()
There's no need to code:
Animal lion = new Animal()
lion.getFur();
If getFur is static, you can just do:
Animal lion;
lion.getFur();
No need to get deeper, if they want to know deep concepts, they'll ask.
When the Object gets created its Class Instance is located in JVM Heap Memory the reference (The Address of the Object Heap ) is Stored in the Stack memory. All the Non Static elements of the class resides there only in the Stack of JVM. To access the local members of the Stack the JVM will require the object in the heap first then it gets the reference of the variable and methods from the stack. Hence for local members, Object creation is Important. As Static Lives in the HEAP only the JVM can access it directly. In this scenario object creation is not required. It gets the reference of elements directly from the heap only. That's the reason variable remains constant across classes and methods can be accessed without object creation.
I said that static methods are loaded first into the JVM and gave the example of main. Also said that they are class methods and an instance of the class is not required to call them. (this is the answer he is looking for)
- perllove January 19, 2008