Wednesday, May 14, 2008

Uses of Static in Java


static variables and methods might better have been called perClass variables and methods. They inherited this misleading terminology from C++. They are the opposite of instance variables and methods that work on a particular object.
There is nothing static (unchanging) about them. They don’t cling. They are perfectly clear, unlike radio signals garbled by static.

They are allocated when the class is loaded. static refers to a method or variable that is not attached to a particular object, but rather to the class as a whole.
static final when applied to a variable is Javanese for "constant". All static methods are automatically final. It is not strictly speaking an error to mark them final, but it is redundant and considered bad form.
static methods work without any this object. static methods are limited to calling other static methods in the class and to using only static variables. They can call instance methods only if they use their own object references -- not rely on this .
static methods and variable are in a sense inherited, but not in the same strong sense that instance variables and methods are. You can refer to Dog.bark() as Dalmatian.bark() if no one has written a Dalmatian.bark(). However, if you use Dog.bark() you always get the Dog version and if you say Dalmatian.bark() you always get the Dalmatian version.
Newbies tend to overuse static variables. Consider what would happen if your code were used by several threads simulaneously. With shared static variable they would trip over each other. With local and instance variables they often would not, even without any special sychronisation. Sometimes, of course, you do need the globalness of static variables, but don’t use it where it would make more sense to create a object to track each chain of calculation.
Static typing
The word static is used in a second context, the opposite of dynamic or runtime type. This refers to the compile-time declared type of a variable, compared with the run time actual type it points to. e.g. a Dog variable may point to a Dalmatian object, but not vice versa. The static type (the type of the reference) is Dog and the dynamic type (the type of the object) is Dalmatian. You will often hear Java referred to a language with static type checking. The types of all references are checked for consistency at compile time.
Static Loading
The word static is used in a third context. It refers to ordinary classes explicitly mentioned in the code. Dynamic classes are loaded on-the-fly using Class.forName where the class name is constructed as a String.
Static Web Pages
The word static is used in a fourth context. It refers to web pages the server sends out unchanged. Dynamic pages are modified or even composed from scratch before sending.
Static Nested Classes
Finally, Java has an obscure feature where you can also declare nested classes static. This does not mean all their methods are static, or that they cannot be instantiated, rather it means you instantiate the inner classes independently of the main enclosing class. There is no associated outer class object. Such classes are often called nested static classes. Non-static inner class objects always have an associated outer class object.


Uses of Final Keyword


Final Variables
A variable can be declared final. A final variable may only be assigned to once. It is a compile time error if a final variable is assigned to unless it is definitely unassigned immediately prior to the assignment.

A blank final is a final variable whose declaration lacks an initializer.


Once a final variable has been assigned, it always contains the same value. If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object. This applies also to arrays, because arrays are objects; if a final variable holds a reference to an array, then the components of the array may be changed by operations on the array, but the variable will always refer to the same array.
Declaring a variable final can serve as useful documentation that its value will not change and can help avoid programming errors.
In the example:

class Point {
int x, y;
int useCount;
Point(int x, int y) {
this.x = x;
this.y = y;
}
final static Point origin = new Point(0, 0);
}

the class Point declares a final class variable origin. The origin variable holds a reference to an object that is an instance of class Point whose coordinates are (0, 0). The value of the variable Point.origin can never change, so it always refers to the same Point object, the one created by its initializer. However, an operation on this Point object might change its state-for example, modifying its useCount or even, misleadingly, its x or y coordinate.
We call a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression, a constant variable. Whether a variable is a constant variable or not may have implications with respect to class initialization , binary compatibility and definite assignment .

final Fields
A field can be declared final. Both class and instance variables (static and non-static fields) may be declared final.
It is a compile-time error if a blank final class variable is not definitely assigned by a static initializer of the class in which it is declared.
A blank final instance variable must be definitely assigned at the end of every constructor of the class in which it is declared; otherwise a compile-time error occurs.

final Methods

A method can be declared final to prevent subclasses from overriding or hiding it. It is a compile-time error to attempt to override or hide a final method.
A private method and all methods declared immediately within a final class behave as if they are final, since it is impossible to override them.
It is a compile-time error for a final method to be declared abstract.
At run time, a machine-code generator or optimizer can "inline" the body of a final method, replacing an invocation of the method with the code in its body. The inlining process must preserve the semantics of the method invocation. In particular, if the target of an instance method invocation is null, then a NullPointerException must be thrown even if the method is inlined. The compiler must ensure that the exception will be thrown at the correct point, so that the actual arguments to the method will be seen to have been evaluated in the correct order prior to the method invocation.
Consider the example:
final class Point
{
int x, y;
void move(int dx, int dy) {
x += dx; y += dy;
}
}

class Test {
public static void main(String[] args) {
Point[] p = new Point[100];
for (int i = 0; i < p.length; i++) {
p[i] = new Point();
p[i].move(i, p.length-1-i);
}
}
}
Here, inlining the method move of class Point in method main would transform the for loop to the form:
for (int i = 0; i < p.length; i++) {
p[i] = new Point();
Point pi = p[i];
int j = p.length-1-i;
pi.x += i;
pi.y += j;
}


The loop might then be subject to further optimizations.
Such inlining cannot be done at compile time unless it can be guaranteed that Test and Point will always be recompiled together, so that whenever Point-and specifically its move method-changes, the code for Test.main will also be updated.


final Classes
If a class that was not declared final is changed to be declared final, then a VerifyError is thrown if a binary of a pre-existing subclass of this class is loaded, because final classes can have no subclasses; such a change is not recommended for widely distributed classes.
Changing a class that was declared final to no longer be declared final does not break compatibility with pre-existing binaries.


Friday, May 9, 2008

What are the OOPS concepts


1) Encapsulation: It is the mechanism that binds together
code and data in manipulates, and keeps both safe from
outside interference and misuse. In short it isolates a
particular code and data from all other codes and data. A
well-defined interface controls the access to that
particular code and data.

2) Inheritance: It is the process by which one object
acquires the properties of another object. This supports
the hierarchical classification. Without the use of
hierarchies, each object would need to define all its
characteristics explicitly. However, by use of inheritance,
an object need only define those qualities that make it
unique within its class. It can inherit its general
attributes from its parent. A new sub-class inherits all of
the attributes of all of its ancestors.
3) Polymorphism: It is a feature that allows one interface
to be used for general class of actions. The specific
action is determined by the exact nature of the situation.
In general polymorphism means "one interface, multiple
methods", This means that it is possible to design a
generic interface to a group of related activities. This
helps reduce complexity by allowing the same interface to
be used to specify a general class of action. It is the
compiler's job to select the specific action (that is,
method) as it applies to each situation


What is MVC Architecture


The main aim of the MVC architecture is to separate the business logic and application data from the presentation data to the user.
Here are the reasons why we should use the MVC design pattern.
They are resuable : When the problems recurs, there is no need to invent a new solution, we just have to follow the pattern and adapt it as necessary.

They are expressive: By using the MVC design pattern our application becomes more expressive.
1). Model: The model object knows about all the data that need to be displayed. It is model who is aware about all the operations that can be applied to transform that object. It only represents the data of an application. The model represents enterprise data and the business rules that govern access to and updates of this data. Model is not aware about the presentation data and how that data will be displayed to the browser.
2). View : The view represents the presentation of the application. The view object refers to the model. It uses the query methods of the model to obtain the contents and renders it. The view is not dependent on the application logic. It remains same if there is any modification in the business logic. In other words, we can say that it is the responsibility of the of the view's to maintain the consistency in its presentation when the model changes.
3). Controller: Whenever the user sends a request for something then it always go through the controller. The controller is responsible for intercepting the requests from view and passes it to the model for the appropriate action. After the action has been taken on the data, the controller is responsible for directing the appropriate view to the user. In GUIs, the views and the controllers often work very closely together.
Difference between Model 1 and Model 2 architecture:
Features of MVC1:
Html or jsp files are used to code the presentation. To retrieve the data JavaBean can be used.
In mvc1 archictecture all the view, control elements are implemented using Servlets or Jsp.
In MVC1 there is tight coupling between page and model as data access is usually done using Custom tag or through java bean call.
Features of MVC2:
The MVC2 architecture removes the page centric property of MVC1 architecture by separating Presentation, control logic and the application state.
In MVC2 architecture there is only one controller which receives all the request for the application and is responsible for taking appropriate action in response to each request.