Friday, September 17, 2010

Core java Interview Questions

1. When should we use static operations?
 
Ans:- In java We need One area where a static context is necessary is During program start-up because the Java entry point method is static is public static void main(String[]).It reveals that the variables and methods requires during start-Up will need to be static too. So static variables are used to hold the class level data and these static variables are maintains the state of the class. In general we use these static variables for the purpose of storing the information like, MAX_LIMIT, MIN_LIMIT as fixed valued variables at class level shared by all the instances of that particular class. These static variables are also used as counter that keeps track of a value across all instances.
          Static methods are often used as utilities that are not associated with a particular instance of a class. For examples static utility methods may be used to parse an input value to another type, as with the Integer.valueOf(String) method, or as so-called factory methods to acquire a pre-configured instance of a type, like the Calendar.getInstance() method.

2. Why main method in java should declare as static? 

Ans:- The main method must be declared public static void because it is invoked when the Java Virtual Machine first starts up, before any Java object instances exist. The main method may reference any number of static variables and call static methods. Any series of calls to other static methods only extends this original static context. To reference non-static or instance variable from the main method, you must first instantiate the relevant object.

3. How to call a static method and members of one class in another class.?

Ans:- Calling static members in a separate class is very similar to calling a static method in a same class. All of that, the static member must have a public, package or protected modifier that makes it accessible to the calling class. Use the class name, a dot separator and the name of the member variable, as with the AWT Color class properties below:

Color textColor = Color.black;
Color panelColor = Color.lightGray;

          The only way that a class can call a method without an object reference is in a static context. For example, the static main () method is invoked in a static context, before any instance is created, so any methods it calls must also be static. The main () method may call static methods of its own, or of other classes.

4. How to cal a static method of one class in another?

Ans:- To call a static method in a separate class you should prefix the method reference with the name of the class it belongs to, known as the host class. For example, the String class has valueOf() methods to convert primitive values to strings:

String booleanString = String.valueOf(true);

5. Can I call a static method from an object reference?

Ans:- Yes, it is possible to call a static method on an object reference using the same dot syntax as for a static class reference, as below.

String stringRef = "Exe";
System.out.println(stringRef.valueOf(true)); 

6. What is the difference between static and final keywords?

Ans:- The static and final modifiers have quite distinct purposes in Java, though they can be used in combination to declare class constants. When the static modifier is applied to a variable or method it belongs to all instances of the class and can be referenced in a static context. In other words, the class does not have to be instantiated before the variable can be read or the method called. That means that static variables must be assigned at compile time and static methods cannot reference instance variables or call instance methods. The final modifier can be applied to a class definition or method to prevent extension or overriding respectively. When the final modifier is declared on a variable it means that its initial assignment value will not change during the execution of the code. Final instance variables must be assigned at compile time or in the class' constructor.

7. What is the difference between a static and a non-static inner class?

Ans:- A non-static inner class may have object instances that are associated with instances of the class's outer class. A static inner class object can be created without Outer class object.

No comments:

Post a Comment