A Java Geek's Diary
A Java Geek's Diary
Enjoying Every Sip Of My Cup...
All | General | Java | Music | SCJP | Design-Notes

Monday September 27, 2004


1. A class declaration specifies a new named reference type.

2. Class Modifier class identifier Super Interfaces Class body. Identifier indicates the class name.

3. Class Modifier: A class declaration may include Class modifer.

4. Public, protected, private, abstract, static, final, strictfp

5. Abstract class: An abstract class is incomplete.

a. An abstract class may have abstract methods which are not implemented.

b. A class which is not abstract should not contain abstract methods. If there compile time error occurs.

c. A class type should be declared as "abstract" only if the sub-classes can provide the implementation. If the intent is to simply prevent from instantiation make constructor private.

6. Final Classes: A class can be declared final if the class's definition is complete and no subclasses are required.

a. If you subclass the final class compile time error occurs.

b. You cannot have a class with both "final" and "abstract".

7. Inner classes and enclosing instances: An Inner class is nested class which is not declared as static either explicitly or implicitly.

a. Inner classes cannot declare static initializers.

b. Inner classes cannot have member interfaces.

c. Inner classes may not declare static members.

d. Inner classes can have compile-time constant fields.

e. Inner classes may inherit static members that are not compile time constants.

8. Types of Inner classes:

a. Top-Level nested classes and interfaces

b. Non-static inner classes

c. Local classes

d. Anonymous classes

9. Super classes and Subclasses:

a. A class is said be direct subclass of the class it extends.

b. The direct super class is the class whose implementation, the implementation of the current class derives.

c. There should not be any extend class for Object because Object itself is Super class.

d. A final class cannot be extended / Sub classed.

10. Super Interfaces:

a. The optional implements clause in the class declaration lists the names of interfaces that are direct super interfaces.

b. A compile time error occurs if the name of the interface is defined for more than one time.

11. Class Body and Method declarations: A class body can contain fields, classes, interfaces, methods, instance initializers, static initializers and constructors.

12. Class Members:

a. Members inherited from super class. (Except for Object)

b. Members inherited from super interfaces.

c. Members declared in the body of class.

d. Members declared private cannot be inherited to sub classes except Protected and Public.

e. Constructors, static initializers, instance initializers, are not members therefore cannot be inherited.

13. Filed Declarations: The variables of class type are introduced by field declarations.

a. You cannot declare two field variables with the same name.

b. Method types and field names may have same name.

14. Field Modifiers:

a. Static: If the field is declared static there will be exactly one incarnation of the field regardless of the class instances. If the field is not declared static then the instance of the field is created for each instance of the class created.

b. Final fields: A class variable or instance variable can be declared final as long as it is assigned some value.

c. Transient Fields: Fields declared with Transient cannot be part of persistence mechanism.

d. Volatile Fields: A field declared Volatile needs to reconcile with the master copy before it is being accessed.

15. Field Initialization:

a. For a class variable the variable initializers are executed only once.

b. For instance variables the variables initializers are executed for every instance of class is created.

c. Restriction on use of Fields during initialization: The declaration of a member needs to appear before it is being used.

16. Method Declaration: Method declares executable code that can be invoked by passing fixed number of arguments.

a. Formal Parameters: A list of parameters with comma separated and type final is optional.

b. The scope of these parameters will be in the entire body of the method.

c. Method Signature: The signature of the method is the Method name and number and type of formal parameters.

17. Method Modifiers: Methods can have public, private, protected, abstract, static, final, synchronized, native, strictfp.

a. A method declaration with abstract cannot have private, static, final, native, strictfp, or synchronized.

b. A method with native and strictfp causes compilation error.

18. Abstract Methods: An abstract method is declared with method signature, return type, exception thrown if required but does not provide any implementation.

a. The abstract method must be declared in the abstract class.

b. The implementation for the abstract class should be given by the first subclass which is not abstract.

c. Compilation error occurs if the abstract method declared private.

d. Compilation error occurs if the static method declared abstract.

e. Compilation error occurs if the final method declared abstract.

19. Static Methods: A method declared static is called Class method.

a. Static methods cannot access the instance variables/ methods.

20. Final Methods: A method can be declared final to prevent it from sub classing.

a. A private method implicitly final

b. A method declared final cannot be abstract

c. Usage of making final: Compiler in lines the body of the code and when ever the method is called the method is replaced with the code

21. Native Methods: A method signature with native keyword and a semicolon at the end of the signature means that the method is implemented by some other programming language like c, c++, fortran.

a. A method with native keyword should not have abstract keyword.

22. Strictfp: The effect of the keyword Strictfp is to make the expression like float and double to follow FP- Strict.

23. Synchronized: A method declared with synchronized is acquired with a lock and only accessed by that process. Other processes which are waiting for the method lock have to wait until the lock obtained by the process releases.

24. Throws: A throws clause is used to declare any checked exceptions that can result from the execution of a method constructor.

a. Checked Exceptions: Are kind of exceptions where developer has to assume the arising of the exception upfront and is declared, otherwise compiler gives an error.

b. Runtime Exceptions: Are kind of exceptions where you cannot predict them like out of memory exception, Null Pointer Exception.

c. A method that overrides/ hides/ implementation for abstract method should not throw more exception than the overridden / hidden methods.

25. Instance Initializers: An instance initializer is declared in a class is executed when an instance of the class is created.

a. Instance initializers are an anonymous class which can throw any type of exception.

b. Compile time error occurs if the instance initializer is not completed.

c. Compile time error occurs if there exist any return type in the block.

26. Static Initializers: Any static initializers declared in a class are executed when the class is initialized.

a. Compile time error occurs if the static initializer is not completed.

b. Compile time error occurs if there exist any return type in the block.

c. All the declarations appeared are textually executed.

d. If the this keyword/ super() keyword exists in the block then the compile time error occurs.

27. Constructor Declaration: A constructor is used in the creation of class instance.

a. A constructor should be similar to method signature except the result type is not mentioned.

b. Access to Constructors is governed by the access modifiers.

c. Constructors can be overloaded but never overridden.

d. Formal parameters for the constructor are similar to method parameters.

e. Constructor can have Private, Public, Protected types as access modifiers.

f. Abstract, static, final, native, strictfp, synchronized should not be used with Constructor.

g. Constructor body first statement can invoke another constructor of the same class/ super class.

h. Alternate constructors are invoked with this keyword.

i. Super class constructors are invoked with super keyword.

j. If a class does not contain a constructor declared then the default constructor is invoked.

k. A class declared with private constructor cannot be instantiated.


Sunday September 26, 2004

Inner Classes

1. Top level Nested classes and Interfaces: A top level nested class/interface is similar to the top level package member class/interface except it is defined as Static of an enclosing top level class/interface.

a. Top level nested classes and interfaces can be defined to ant depth but only within the other static top level classes/ interfaces.

b. A static nested top level class can be instantiated with out any reference to the instance of the enclosing context.

c. A method in the top level nested class can access directly the static members of the enclosing context but not the instance of the enclosing context.

d. Static methods don't have "this" reference so only static methods and variables are accessed. A method in a nested top level class can access directly the static members in the enclosing class/ interface but not instance members.

e. Top level Nested class can define both Static and instance members like any other package level classes. But these classes can access only the static members in its enclosing context.

f. A top level nested class being the member of enclosing class can have all the accessibility. (Public, Private, and Protected)

g. The Top level nested class/ Interfaces classes are separated with '$'

2. Non-Static Inner Classes: A non static inner class is defined without the Static keyword as a member of enclosing class.

a. An instance of non-static inner class exists only with the instance of the enclosing class. This means non-static inner class cannot have static members.

b. Methods of non static inner class can access refer to any member of the any enclosing class.

c. Non -static inner class is member of enclosing class it has accessibility Public, Private and Protected members.

d. Shadowing the members of Non Static inner classes: A special form of the syntax should be used to access the members enclosing context.

e. Inheritance and Containment: Non Static inner classes can extend other classes and can themselves is extended.

3. Local Classes: A local class is a class defined in a block. This block could be Method body, Constructor, a local block, static initializers, instance initializers.

a. The visibility is up to that block.

b. A local class cannot be specified with keyword static. But if the block itself is static the local class implicitly becomes static.

c. A non-static local class works similar to the non-static inner class except with some restrictions.

i. Local classes cannot have static members.

ii. Local classes cannot have accessibility.

iii. Local classes can access only final local variables, final method parameters, and final catch block parameters of the local context.

d. Instantiating local classes:

i. Clients outside the local class cannot create/ access these classes directly, because they are local.

ii. A local class can be instantiated in the block in which it is defined.

iii. Creating an instance of non static local class requires an instance of the enclosing class.

4. Anonymous classes: Classes are first defined and then instantiated with new keyword. Anonymous classes include both at a time. It means Classes of type. Anonymous are defined and instantiated at the same time with additional keyword operator new. This happens because these classes don't have name.

a. Anonymous classes are used to create objects on the fly.

b. The usage can be at various places like method parameters, initialization of variables.

c. Major usage is to extend the Adapter classes.

d. If the return type of the Anonymous class is Static then the Anonymous class is Static otherwise it is non-static.

e. Access Rules for Anonymous classes:

i. Non - static inner classes can access the entire members in the enclosing context and final variables.

ii. Like local classes anonymous classes cannot have static members and they cannot specify any accessibility modifiers.


Monday September 20, 2004


1. Programs are organized as sets of Packages. Packages help us in preventing name conflict.

2. Packages contain Classes, Interfaces and Sub-Packaged which intern again contains the Classes, Interfaces and Sub-Packages.

3. A Package contains set of compilation units. Each compilation unit can access all other types declared in that package.

4. Package Members: The members of the package are Sub-Packages, Top-level classes and Top-level interfaces.

Ex: Java has sub-packages like awt, lang, and io which does not have compilation units.

5. Fully Qualified Package Name: If P is a package and Q is a sub package of P then P.Q is the fully qualified name of the sub-package.

6. A package may not contain two members of the same name causes compilation errors.

Ex: A package with name java.awt has sub-package image. Then there should not be any declaration of class or interface type named image.

7. Compilation Units: It is a global syntactic grammar of java programs. It has three different parts.

a. Package Declaration

b. Import Declaration

c. Top Level Declaration

8. Package Declaration: This indicates the package to which the compilation unit belongs to.

Ex: package "Package Name" (Fully Qualified)

a. A compilation unit that has no package declaration is part of unnamed package.

b. Package Observable iff A compilation unit containing a declaration of the package is observable. A sub-package of the package is observable.

c. Scope of the declaration of an observable top level package is all observable units. The declaration of the package that is not observable is not in scope.

9. Import Declaration:

a. Single Type Import Declaration: This import the specific type mentioned canonical name.

Ex: java.util.Vector;

b. Type Import on demand Declaration: Imports all the accessible types. All public classes are available.

Ex: java.util.*;

10. Top level declarations:

a. This declares a Top level Class/ Interface types.

b. A compile time error occurs if the name of the Top level appears as the name of the any other top level class or interface.

11. Unique Package Names: Use unique package names where you can easily install them.

12. Package name should be always first statement if it all it is appearing.

13. There should be only one package statement for file.

Here is a very good article on how to use Packages.


Sunday September 12, 2004

One could learn the Java keywords with a poem

Abstract boolean, break the byte!

(break the scheme)

case and catch and "char" with class!

(have your own style)

const continue, default do.

(don't be like the others)

"double else" extends final

(thinking twice make things harder (?))

(remember to pronounce the S here)

finally, float for goto

if implementS import

(if you need a strong programming implement, rebel and try "goto" (this is ironic,of course. Pronounce the S here, don't forget))

instanceof int: interface

(talks for itself)

Long native? New package!

(Change old traditions for new technologies)

(now it comes the superhero saga part)

"Private Protected Public" return!!

(in part I, Private Java protected the public classes city. Now, the saga continues)

Short, static, strictfp

(a description of his braveness fp: firm precission (?) )

super switch synchronized this


throw, throws transient


try "void, volatile" while assert

(He can dissapear)

What do you think? No... please don't tell the truth.

Courtesy: JavaRanch


Friday September 10, 2004

Difference Between equals() and compareTo()

I found some of my co-developers using compareTo instead of using equals to check equality of object check. This is interesting because the actual purpose is solved for them as the result is coming.

What I find after doing some reading on difference is both the methods are completely different.

1. equals() is from java.lang.Object and used for comparing objects.

2. CompareTo() is derived from Comparable interface used to find the Ordering of Strings.

Here is what API says

public boolean equals(Object anObject)

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

public int compareTo(String anotherString)

Compares two strings lexicographically. ....The result is a negative integer if this String object lexicographically precedes the argument string. Theresult is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.

Logically both are meant for different purposes but when it comes to usage I dont know if we can use compareTo() instead of equals(). What do you say?


Sunday September 05, 2004

Thread States

There is always high possibility to do fundamental mistakes while taking SCJP exam. I would be writing some notes for myself on different topics and will be posting them here. I am reading some of the topics little deep from Javarules book. It is huge book :)

New: This is the state when the thread instance is created but the start method is not invoked on thread. At this point thread is not alive.

Runnable: This is the state where the thread is eligible to run, but the scheduler has not selected it to be the running thread. Thread enters into Runnable state when it comes the start() method is invoked. A thread can come back to Runnable state from Waiting, Sleeping, and Blocked states.

Running: This is the state the thread is selected from the Runnable pool of threads and is executed.

Waiting/Blocking/Sleeping: A thread is still alive but is not currently eligible for running. It returns to the Runnable pool after an event is completed/occured.

a. Blocking may cause because of waiting for a resource.

b. Waiting may cause because the thread is told to wait for sometime.

c. Sleeping may cause because the thread code tells to sleep.

Dead: A thread is considered as Dead when it completed the run() method. Once the thread is completed/dead it can never be bought again. Calling back the dead thread causes the runtime exception.

Preventing Thread Execution:

Sleep: A static method of class Thread. You can use it in your code to slow down by forcing a thread to go into sleep.Ex: Thread.sleep(1000); Milliseconds

Yield: Not guaranteed to do much of anything typically it causes the currently moving thread to runnable state and thread of same priority is can have chance.

Join: Guaranteed to cause the current thread to stop executing until the thread it joins with completes.

Thread Priorities: A thread can be set with priority but not guaranteed in the behavior. Priorities are set in between 1 to 10

a. Thread.MIN_PRIORITY (1)

b. Thread.MAX_PRIORITY (10)

c. Thread.NORM_PRIORITY (5)

Class Object:

1. wait() Make the thread in waiting pool

2. notify() Sends signal to the thread that is waiting on the same object.

3. notifyAll() Sends signal to all the threads that are waiting on the object

Class Thread:

1. start()

. yield()

3. sleep()

4. join()

Interface Runnable:



Member of
Java Blogs
Java Rss
Java Tips
Hitech City JUG
Hyderabad INDIA
I read
I write

Technorati Profile Get Firefox!