Wednesday, March 30, 2011

Java Transient Variables

If you mark an instance variable as transient, you're telling the JVM to skip (ignore) this variable when you attempt to serialize the object containing it. Serialization is one of the coolest features of Java; it lets you save an object by writing its state to the serialisation stream.

Java Instance variables and Class variables

aka static/non-static variables.

In object-oriented programming with classes, an instance variable is a variable defined in a class (i.e. a member variable), for which each object of the class has a separate copy. They live in memory for the life of the class.

An instance variable is the opposite of class variable, and it is a special type of instance member.

A class variable is a variable defined in a class (i.e. a member variable) of which a single copy exists, regardless of how many instances of the class exist. We use keyword static for declaring class variables.

Instance variable:
■ Can be marked final
■ Can be marked transient
■ Cannot be marked abstract
■ Cannot be marked synchronized
■ Cannot be marked strictfp
■ Cannot be marked native
■ Cannot be marked static, because then they'd become class variables.

Java default values

There is a simple rule - when you define a member variable, it gets a default value. When you define local value, it do not get any default value - it has to be inicialized before the first use, or an compile-time exception will be thrown.

Default values for member variables:

Data Type Default Value (for fields)
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char '\u0000'
Any object null
boolean false

Java constructors

Here are some legal and illegal examples:

class ConstructorExamples {
    // legal constructors
    ConstructorExamples () { }
    private ConstructorExamples (byte b) { }
    ConstructorExamples (int x) { }
    ConstructorExamples (int x, int... y) { }

    // illegal constructors
    void ConstructorExamples () { }           // it's a method, not a constructor
    ConstructorLike () { }                    // not a method nor a constructor
    ConstructorExamples (short s);            // looks like an abstract method
    static ConstructorExamples (float f) { }  // can't be static
    final ConstructorExamples (long x) { }    // can't be final
    abstract ConstructorExamples (char c) { } // can't be abstract
    ConstructorExamples (int... x, int t) { } // bad var-arg syntax
}

Java Methods with Variable Argument Lists, var-args

As of 5.0, Java allows you to create methods that can take a variable number of
arguments. This feature is also known as "var-args", "varargs", "variable-length argument lists" or "variable arguments,".

Var-args declarations
Here are some legal and illegal var-arg declarations:

Legal:
void myMethodX( int... a ) { } // expects from 0 to many ints
                             // as parameters
void myMethodY( char c, int... a ) { } // expects first a char,
                                     // then 0 to many ints
void myMethodZ( Animal... animal ) { } // 0 to many Animals
void myMethodXYZ( Object... anyKindOfObjects ) { } 
                            // 0 to many Objects

Illegal:
void myMethodA( int x... ) { }              // illegal syntax
void myMethodB( String... str, byte b ) { } // var-arg must be last
void myMethodC( int... x, char... y ) { }   // too many var-args

Accessing var-args
Example 1:
    public static void test(int some, String... args) {
        System.out.print("\n" + some);
        for(String arg: args) {
            System.out.print(", " + arg);
        }
    }


Example 2:
static int sum (int ... numbers)
{
   int total = 0;
   for (int i = 0; i < numbers.length; i++)
        total += numbers [i];
   return total;
}

Java HttpServletResponse

HttpServletResponse is an interface from package javax.servlet.http. It extends javax.servlet.ServletResponse.

The important interface members are:
* addCookie(Cookie cookie);
* addHeader(String name, String value);
* sendError(int sc) Sends an error response to the client using the specified status code and clearing the buffer;
* sendError(int sc, String msg) Sends an error response to the client using the specified status.
* sendRedirect( String newLocation ) throws IOException - Sends a temporary redirect response to the client using the specified redirect location URL. Realised by http code 302 (Redirect).
* setHeader(String name, String value);
* setStatus(int sc) Sets the status code for this response.

Noticable methods from the super interface javax.servlet.ServletResponse:
* String getCharacterEncoding();
* String getContentType() - Returns the content type used for the MIME body;
* boolean isCommitted();
* setCharacterEncoding(String charset);
* setContentType(String type);

Tuesday, March 29, 2011

Java ServletContext

ServletContext is an interface from package javax.servlet;

Getting ServletContext
ServletContext could be get by session.getServletContext() method.
HttpSession could be get by request.getSession( boolean create );

The most important members are:
* addFilter(java.lang.String filterName, Filter filter) - Registers the given filter instance with this ServletContext under the given filterName;
* addListener(java.lang.Class listenerClass) - Adds a listener of the given class type to this ServletContext;
* Object getAttribute(java.lang.String name);
* ClassLoader getClassLoader();
* String getContextPath();
* getMimeType(java.lang.String file);
* log( String message, Throwable throwable );
* setAttribute( String name, java.lang.Object object );