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.

Wednesday, September 15, 2010

static keyword in java...

  •  When a variable is declared with the static keyword, its called a class variable. All instances share the same copy of the variable. A class variable can be accessed directly with the class, without the need to create an instance.
  • Without the “static” keyword, it's called instance variable, and each instance of the class has its own copy of the variable.
  • Static block are execute even before the constructors are execute.
  • When the class is first loaded static blocks can be called once.
  • Static members can be execute without object creation.The following example
    illustrates about the static variables, static methods and static blocks.

Ex:-

 public class Test
{  
    public static void main(String[] args)
    {
        StaticDemo sd = new StaticDemo();
        StaticDemo.staticMethod();
    }
}

class StaticDemo
{
    public static final int x=10;
    public static final int y;
    static
    {
        y=20;
        System.out.println("X value is = "+x+" \nY value is = "+y);
    }
    public static void staticMethod()
    {
        System.out.println("Inside the static method of StaticDemo class");
    }
    StaticDemo()
    {
        System.out.println("Inside the constructor of staticDemo class");
    }
}


Output::

X value is = 10
Y value is = 20
Inside the constructor of staticDemo class
Inside the static method of StaticDemo class

          In the above example demonstrates that the order of execution is.

          >>  static variables initialize first then static block will be executed and then static
                 method will be  executed.
          >> Here constructor is called finally . This reveals that all static content can be
                execute before object  creation.

          >> The static method can access only static data
          >> It is illegal to refer a instance variable inside the static method or static block, that
                means it will be compilation error.
          >> static methods and static variables can be called  y using class name with "."
                operator like

                 Test.x;
                 Test.staticMethod();

                 Here "Test" is the class which contains the static variable "x" and static method
                 "staticMethod()"
  • Methods can also be declared with the static keyword. When a method is declared static, it can be called/used without having to create a object first. For example, you can define a collection of math functions in a class, all static, using them like functions
  • Static methods can only call the other static methods
  • Methods declared with “static” keyword are called “class methods”. Otherwise they are “instance methods”.
  • There's no such thing as static classes. “static” in front of class creates compilation error, but inner classes can be declared as static 

Static Inner Classes
  • A class itself is not a static class . But we can declare a nested class as static. Here the static keyword    in the class says that the nested class is a static member of the outer class which contains the inner class  . For example

Ex:-

      class Outer
{
    static class InnerCls {
        void hi()
        {
            System.out.println("Hi");
        }
    }
}

public class InnerDemo
{
    static class InnerDC {
        void dcHi()
        {
            System.out.println("DC HI");
        }
    }
    public static void main(String[] args)
    {
        Outer.InnerCls inner = new Outer.InnerCls();
        inner.hi();
        InnerDC innerdc = new InnerDC();
        innerdc.dcHi();
    }
}

 Output::

    Hi
    DC HI