Saturday, January 19, 2008

Java Tips for ...novice programmers...

Object-Oriented Programming• An abstract method cannot (obviously) be final.
• An abstract method cannot be static because static methods cannot be
overridden.
• An instance method can be both protected and abstract. A static method
can be protected.
• The JVM does not call an object’s constructor when you clone the object.
• Classes can be modified from their default state using any of the three
keywords: public, abstract, and final. So, can’t have a static class,
only static methods.
• A final variable is a constant, and a final method cannot be overridden.
• A call to this in a constructor must also be on the first line.
Note: can’t have an explicit call to super followed by a call to this
in a constructor - only one direct call to another constructor is allowed.


Memory and Garbage Collection• Can’t predict when garbage collection will occur, but it does run
whenever memory gets low.
• If you want to perform some task when your object is about to be
garbage collected, you can override the java.lang.Object method called
finalize(). This method is declared as protected, does not return a value,
and throws a Throwable object, i.e. protected void finalize() throws Throwable.
• Always invoke the superclass’s finalize() method if you override finalize().
• The JVM only invokes finalize() once per object. Since this is the case,
do not resurrect an object in finalize as when the object is finalized
again its finalize() method will not be called. Instead you should create a
clone of the object if you must bring the object back to life.
• Remember that Java passes method parameters by value and not by
reference. Obviously then, anything that happens to a primitive data
type inside a method does not affect the original value in the calling code.
Also, any reassignment of object references inside a method has no effect
on the objects passed in.


Exceptions
• Invoking a method which declares it throws exceptions is not
possible unless either the code is placed in a try-catch, or the calling
method declares that it throws the exceptions, i.e. checked
exceptions must be caught or rethrown. If the try-catch
approach is used, then the try-catch must cope with all of the
exceptions which a method declares it throws.
• RuntimeException and its subclasses are unchecked exceptions.
• Unchecked exceptions do not have to be caught.
• All Errors are unchecked.
• You should never throw an unchecked exception in your own code, even
though the code will compile.

Methods

• A native method does not have a body, or even a set of braces,
e.g. public native void method();
• A native method cannot be abstract.
• A native method can throw exceptions.
• A subclass may make an inherited method synchronized, or it may
leave offthe synchronized keyword so that its version is not synchronized.
If a methodin a subclass is not synchronized but the method in the
superclass is, the threadobtains the monitor for the object when it enters
the superclass’s method.• You cannot make a method in a subclass more
private than it is defined in thesuperclass, ‘though you can make it more public.


Threads

• A Java program runs until the only threads left running are daemon threads.
• A Thread can be set as a user or daemon thread when it is created.
• In a standalone program, your class runs until your main() method
exists - unless your main() method creates more threads.
• You can initiate your own thread of execution by creating a Thread
object, invoking its start() method, and providing the behaviour that
tells the thread what to do. The thread will run until its run() method
exists, after which it will come to a halt - thus ending its life cycle.
• The Thread class, by default, doesn’t provide any behaviour for run().
• A thread has a life cycle. Creating a new Thread instance puts the thread
into the "new thread" state. When the start() method is invoked, the
thread is then "alive" and "runnable". A thread at this point will repond
to the method isAlive () by returning true.
• The thread will continue to return true to isAlive() until it is "dead",
no matter whether it is "runnable" or "not runnable".
• There are 3 types of code that can be synchronized: class methods, i
nstance methods, any block of code within a method.
• Variables cannot take the synchronized keyword.
• Synchronization stays in effect if you enter a synchronized method
and call out to a non-synchronized method. The thread only gives
up the monitor after the synchronized method returns.



Inner Class

• If you define an inner class at the same level as the enclosing class’
instance variables, the inner class can access those instance variables -
no matter what their access control.
• If you define an inner class within a method, the inner class can
access the enclosing class’ instance variables and also the local
variables and parameter for that method.
• If you do reference local variables or parameters from an inner
class, those variables or parameters must be declared as final to
help guarantee data integrity.
• You can also define an anonymous class - a class without a name.
• If you’d like to refer to the current instance of the enclosing class,
you can write EnclosingClassName.this.
• If you need to refer to the inner class using a fully qualified name,
you can write EnclosingClassName.InnerClassName.
• If your inner class is not defined as static you can only create new
instances of this class from a non-static method.
• Anonymous classes cannot have const


Serializing Objects

•It is now possible to read and write objects as well as primitive data
types, using classes that implement ObjectInput and ObjectOutput.
These two interfaces extend DataInput and DataOutput to read or
write an object. ObjectInputStream and ObjectOutputStream
implement these interfaces.
• If a class implements the Serializable interface, then its public and
protected instance variables will be read from and written to the
stream automatically when you use ObjectInputStream and
ObjectOutputStream.
• If an instance variable refers to another object, it will also be
read or written, and this continues recursively.
• If the referenced object does not implement the Serializable
interface, Java will throw a NotSerializableException.
• The Serializable interface serves only to identify those instances
of a particular class that can be read from and written to a stream.
It does not actually define any methods that you must implement.
• If you create your own class and want to keep certain data from
being read or written, you can declare that instance variable
using the transient keyword. Java skips any variable declared as
transient when it reads and writes the object following the Serializable
protocol.


Reflection

• Using the Reflection API, a program can determine a class’ accessible
fields, methods and constructors at runtime.


will be adding moreeeeeeeeeeeee later, guys....... you are also free to add comments on this and more points........

Heroku Custom Trust Store for SSL Handshake

  Working with Heroku for deploying apps (java, nodejs, etc..) is made very easy but while integrating one of the service ho...