Tips N Tricks
1. Just reading about the concepts and seeing examples will not make things clear. Make a small
example of your own to test every new concept/rule you learn. This will help you understand it
better. Also, alter the example and experiment with different possibilities. You will learn and
retain much deeper if you follow this process.
Lets say, you read about the rule that static methods cannot be overridden. It is not very clear
what will happen if you attempt to override a static method, whether this will cause a compiler
error or not.
To explore this, you can make an example similar to this one.
class Base{
static void f() { System.out.println("base"); }}
class Derived extends Base{
static void f() { System.out.println("derived"); }
public static void main(String args[]){
Base base=new Derived();
base.f();
}
}
Now try to compile, there are no errors. So that means it is not illegal to have the same static
method in the base and derived class. But when you invoke the method, the actual method
invoked is that of the base class. This is because the method invoked depends on the type of
the reference variable. Thats why you say that overriding does not happen in this case. If this
had been a non static method, the method invoked would have been the derived one. Now
things are clear to you.
Once this concept is clear, you can start trying different possibilities.
- What happens if you override a static method as non-static or vice versa
- What will happen if you try to declare a static method abstract
- Can a static method be synchronized.
You can think of many options like this. Make small variations to the program and try out all of
them.
Instead of learning just one concept, you will get a deep understanding of many related
concepts.
2. Once you have gone through all the concepts once, start taking as many mock exams as
possible to test your knowledge. However much you have prepared, you will still find weak
areas where you need to concentrate and important points which you missed out. After taking
each exam, note down the mistakes you made. On the day before the exam, you can go
through them once more to make sure that you are clear in those now.
3. The objective on Garbage collection will have questions asking how many objects are eligible
for garbage collection at a particular point in the program. The answers to such questions
cannot be verified with the help of examples because you cannot force the garbage collector to
run. So attempt as many questions as possible on this topic from various mock exams,
compare your answers to test your knowledge.
4. Inner classes is one of the most confusing sections, you need to memorize the correct syntax,
ways of instantiation and access rules. Even questions from other objectives might be using
inner classes. So try to practice a lot in this area.
Anonymous inner class are especially prone to errors.
Watch out for small syntax errors like the missing semicolon at the end of this anonymous
inner class defined inside a method argument.
t.doSomething(new AnotherClass(3) {
public void f(){
System.out.println("something");
}
})
5. Threads and Garbage collection are 2 topics in Java which have some platform/implementation
dependent features. You should be able to clearly differentiate betweeen such behaviour and
features which do follow the rules and hence are predictable.
For example, garbage collection algorithm varies between different implementations. Garbage
collection cannot be forced to happen, you can only request for it. So there are no guarantees
here. Now lets see what you can be sure about. The finalize() method of an object will be
surely called once before it is garbage collected. This rule will not be violated, whatever the
implementation is like. But again, you need to remember that finalize() might never be
invoked on the object because the garbage collector might never run during the program
No comments:
Post a Comment