Các câu hỏi phỏng vấn và câu trả lời về phần "Core Java - OOPs concepts: Initial OOPs" - Phần 2: Thuộc tính và phương thức tĩnh (static)
STATIC INTERVIEW QUESTIONS
1) What is static variable?
2) What is static method?
3) Why main method is static?
Because object is not required to call static method if It were non-static method,jvm creats object first then call main() method that will lead to the problem of extra memory allocation
4) What is static block?
5) Can we execute a program without main() method?
Yes, one of the way is static block
6) What if the static modifier is removed from the signature of the main method?
Program compiles. But at runtime throws an error "NoSuchMethodError".
7) What is difference between static (class) method and instance method?
static or class method |
instance method |
1)A method i.e. declared as static is known as static method. |
A method i.e. not declared as static is known as instance method. |
2)Object is not required to call static method. |
Object is required to call instance methods. |
3)Non-static (instance) members cannot be accessed in static context (static method, static block and static nested class) directly. |
static and non-static variables both can be accessed in instance methods. |
4)For example: public static int cube(int n){ return n*n*n;} |
For example: public void msg(){...}. |
» Tin mới nhất:
» Các tin khác: