Wednesday, June 14, 2006

inner class inside method

Yes, we can have an inner class inside a method and final variables can be accessed.

Transient & Volatile Modifiers

Transient: The transient modifier applies to variables only and it is not stored as part of its object’s Persistent state. Transient variables are not serialized.

Volatile: Volatile modifier applies to variables only and it tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program.

final, finalize() and finally ???

final : final keyword can be used for class, method and variables. A final class cannot be subclassed and it prevents other programmers from subclassing a secure class to invoke insecure methods. A final method can’t be overridden. A final variable can’t change from its initialized value.

finalize() : finalize() method is used just before an object is destroyed and can be called just prior to garbage collection.

finally : finally, a key word used in exception handling, creates a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block. The finally block will execute whether or not an exception is thrown. For example, if a method opens a file upon exit, then you will not want the code that closes the file to be bypassed by the exception-handling mechanism. This finally keyword is designed to address this contingency.

Thursday, June 01, 2006

Unchecked exception

Any exception that extends from RuntimeException is considered an unchecked exception. Unchecked exceptions do not have to be handled in a catch block, though they can be. The compiler never forces you to handle unchecked exceptions.

Checked exception

An exception that the compiler forces code to handle. You can usually accomplish this by using a try/catch block. All checked exceptions inherit from the java.lang.Exception class but do not extend the java.lang.RuntimeException class.

User defined Exceptions

Whenever you create your own exceptions, you follow some common steps.

Although you can certainly add your own methods and variables, it is more common to simply use the inherited methods defined in your superclasses. First, you want to define at least two constructors: a default, no-argument constructor, and a constructor that accepts a String object as a parameter. The latter constructor allows you to pass the message that you want this exception type to return from the getMessage() method. Normally, all these constructors do is invoke the superclass constructors, located in Exception.

Abstract classes

Technically, an abstract class does not have to contain any abstract methods. This is rather unusual, but in such a case, the class simply cannot be instantiated.

> If a subclass does not override all abstract methods, it also must be declared an abstract class. If you try to compile the such class without overriding all abstract methods, the compiler alerts you to this and tells you to either override them or declare this class abstract as well.
> No abstract methods can be declared as private, final, or static.

super() method

The first line of every constructor must call another constructor. If you do not explicitly call another constructor with this() or super(), he compiler always makes super() with no parameters the first line of a constructor.

public ClassName()
{
System.out.println(“Hello!”);
}
public ClassName()
{
super();
System.out.println(“Hello!”);
}

This happens because it is essential that your superclass be initialized before your subclass.

Object casting

When you are performing object casting, the best practice is to incorporate the instanceof operator into your code. First, use instanceof to be sure that the target object is the type you expect; only then should you perform the cast. This ensures that you are never trying to cast an object illegally, and you can be sure to avoid those pesky runtime errors.

Virtual Method Invocation

Although methods can take advantage of virtual method invocation, variables cannot. Variables are always bound to the reference type. This is important to understand if you provide the same variable name in both a superclass and a subclass.

public class SuperVariableDemo
{
public String str = “Superclass”;
}
public class SubVariableDemo extends SuperVariableDemo
{
public String str = “Subclass”;
public static void main(String[] args)
{
SuperVariableDemo sd = new SubVariableDemo();
System.out.println(sd.str);
}
}

The output will always be Superclass even though the runtime type is SubVariableDemo. Remember that instance methods are always bound to the runtime type, and instance variables are always bound to the reference type. If you commit this to memory right now, you will avoid some strange and pesky bugs in the future!

The reference type rule for methods:

The rule is that if you try to call a method via an object reference, that method must be defined in the class of the reference type to begin with. You simply cannot directly call a method solely defined in a subclass if you reference that object with the superclass type.

SuperClass variable

If you define a variable in your subclass with the same name as a variable in your superclass, the superclass variable is hidden. This can lead to hard-to-find errors, so be careful about doing this. The JVM always uses the variable that is the “most local” to the object. If a variable x is defined in both the superclass and the subclass, a subclass instance “sees” only its own variable.

Constructor chaining

Constructors can be “chained” together, allowing one constructor to invoke another and so on until an “ultimate constructor” executes. To chain constructors, you use the this keyword to represent the constructor invocation.

Example of Constructor chaining:

public class ColoredLightBulb2
{
private boolean lit;
private String color;

public ColoredLightBulb2()
{
this(“white”);
}
public ColoredLightBulb2(String color)
{
this.color = color;
}


Two rules for Constructor chaining:

First, if you do invoke another constructor in this fashion, that invocation must come on the first line of your constructor.
The second rule of constructor chaining is that you can explicitly call only one constructor from within another constructor. It is illegal to call another constructor more than once. You could expand the first rule to say that you can invoke another constructor only on the first line and never again in the same constructor body.

Static methods

Static methods cannot access instance methods or instance variables:

Static methods have no such concept. If you think about it, that makes complete sense. Remember that a static method does not require an object to be invoked; normally, static methods are invoked using the ClassName.staticMethodName() syntax. Because a static method can be invoked with no object instance whatsoever, how could there be a this reference?

The rule is that static methods (like the main() method) can never access instance methods or instance variables; static methods can invoke only other static class members directly. Another way to say this is that you cannot invoke an instance method or access an instance variable without an object instance.

pass by value

The truth is that everything—both objects and primitive types—is passed by value. For a primitive type, the value is whatever was assigned to the primitive before it was passed. For an object, the value is the actual object reference. All object references in the JVM are just 32-bit integers that the JVM maps to memory addresses.

switch statement

The switch statement can process only four primitive types: byte, short, char, and int. No other types are allowed for evaluation in a switch statement. Although char may not seem to be an integer, it is. The value it actually holds is the Unicode value, which is a 16-bit integer value. It is represented in code normally as an actual character, of course.

String Basics

String title = “Java”;
title = “Java Foundations”;

Remember, whenever Java “sees” a new string literal, it creates a new object. What is really happening in the previous two lines of code is as follows:
1. The variable title is declared a String.
2. The new String object “Java” is created in memory.
3. This new String object’s reference is stored in the title variable.
4. The new String object “Java Foundations” is created in memory.
5. The reference to this new String object is stored in the title variable.

Math Class

The Math class is final and all the methods defined in the Math class are static, which means you cannot inherit from the Math class and override these methods. Also, the Math class has a private constructor, so you cannot instantiate it.

Need of static in main

The static keyword is needed in the main () so that the interpreter can access the method right from the class instead of requiring an object. As you know of objects so far, you should know that normally a class is just a template for individual objects. A nonstatic method can be called only if an object has been created from a class. A static method can be called without any objects being created.