eProgrammingLab

Java

STATIC MEMBERS AND THEIR CONTROL FLOW
1.	What members are called static members?
The members, which are created outside of a method at class level with static keyword, are called static members. 
2.	What are the types of static members?
Java supports four types of static members:
•	Static Variables
•	Static blocks
•	Static methods
•	Main method
3.	What is static variable?
A class level variable, which has static keyword in its creation statement is called static variable.
•	The static variable can be 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.
•	The static variable gets memory only once in class area at the time of class loading.
4.	What is the advantage of static variable?
Static variable makes your program memory efficient i.e. it saves memory.
5.	What is duplicate variable?
A variable that is created with existed variable name in the scope is called duplicated variable. It leads to compile time error “a variable is already defined”.
6.	Can we create local variable with parameter name?
No, because both parameter and local variables are created in the same method scope. So, local variable is considered as duplicate variable.
7.	What is variable shadowing?
Creating a local variable or parameter with same static or non static variable name is called shadowing. In this case when you access a variable in the method, you will get local variable’s value, but not from class level variable.
8.	What is local preference? How compiler and JVM search for variable definition?
When we call a variable, compiler and JVM search for its creation statement in the current method. If it is not found in the current method, compiler and JVM next search for its creation statement at the class level.
9.	How static variable can be differentiated from local variable or parameter when both have same name?
To differentiate static variables from local variable we should use class name. We can access static variables with class name even through there is no local variable by static variable name. But it is optional.
10.	What is static method?
A method which has static keyword in its creation is called static method.
•	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.
11.	How the static method is executed?
JVM does not execute static methods by itself. They are executed only if they are called explicitly by developer either from main method or from static variable as its assignment statement or from static block.
•	Order of execution of static methods in order they are called, not in the order they are defined.
12.	_______Assigning a static variable with local variable or parameter?
If a local variable or parameter is created with static variable name we must use class name in assigning static variable with that local variable or parameter. Else the modification is stored in that local variable or parameter  not in static variable.
13.	Why java main method is static?
Because object is not required to call static method if it were non-static method, jvm create object first then call main() method that will lead the problem of extra memory allocation.
14.	What is static block?
Static block is a class level nameless block that contains only static keyword in its prototype.
•	It is used to initialize the static data member.
•	It is executed before main method at the time of class loading.
•	Static block is always executed before main method.
•	We can define more than one static block in a class
•	Static keyword is not allowed inside blocks or methods.
•	All static blocks are executed in the order they defined from top to bottom.
15.	What is main method?
Main method is the mediator between java developer and JVM to inform method execution order.
•	It should be called from outside of our package.
•	Main() is the initial point of class logic execution.
•	Hence it should be identified at the time of class loading. Due to this reason it contains static keyword in its prototype.
•	It must be executed without object creation.
•	To execute the main method logic without object creation, method should be defined as static method.
•	If we return a value, it is sent to JVM which is useless. Hence main() method return type  is void as per coding standards the method name should convey the operations it is doing. Since this method is intended to execute main logic of the program, that is business logic, it is named as main.
•	The value passing from keyboard it sent into java application as string type value.