Question-20: What is static variable?
Answer:
Question-21: What is static method?
Answer:
Question-22: why main method is static?
Answer:
because object is not required to call static method if It were non-static method,jvmcreats object first then call main() method that will lead to the problem of extra memory allocation.
Question-23: What is static block?
Answer:
Question-24: Can we execute a program without main() method?
Answer:
Yes,one of the way is, by static block.
Question-25: What if the static modifier is removed from the signature of the main method?
Answer:
Program compiles. But at runtime throws an error “NoSuchMethodError”.
Question-26: What is difference between static (class) method and instance method?
Answer:
Answer:
- static variable is used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
- static variable gets memory only once in class area at the time of class loading.
Question-21: What is static method?
Answer:
- A static method belongs to the class rather than object of a class.
- A static method can be invoked without the need for creating an instance of a class. static method can access static data member and can change the value of it.
Question-22: why main method is static?
Answer:
because object is not required to call static method if It were non-static method,jvmcreats object first then call main() method that will lead to the problem of extra memory allocation.
Question-23: What is static block?
Answer:
- Is used to initialize the static data member.
- It is excuted before main method at the time of classloading.
Question-24: Can we execute a program without main() method?
Answer:
Yes,one of the way is, by static block.
Question-25: What if the static modifier is removed from the signature of the main method?
Answer:
Program compiles. But at runtime throws an error “NoSuchMethodError”.
Question-26: What is difference between static (class) method and instance method?
Answer:
- 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.
- 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(){…}.
check static keyword. you written "that is not unique for all". i think that should be "that is unique for all".
ReplyDelete