Documente Academic
Documente Profesional
Documente Cultură
Methods in an interface cannot be final as they need to be overridden by class implementing the interface.
They are public by default.
‘This’ keyword cannot be used with respect to static methods and variables.
Static variables and static initializer blocks are loaded before main.
Top level classes and methods can never be private and abstract at the same time.
Inner class variables cannot be ‘Static’ unless the class itself is declared as ‘Static’.
Inner class can be a subclass of an outer class and can implement interfaces.
In base class if a non-zero argument constructor is provided and it is subclassed without making any call to
super(args),compiler creates a call to super().Since there is no default constructor in place compilation fails.
e.g :
class A
{
A(int a , int b)
{
}
class B extends A
{
B(int a, int b)
{
//compiler will make an implicit call to super() here since super(a,b) is not provided.
}
}
It is not necessary that if finalize() method is invoked the object will be garbage collected, also it is not
absolutely necessary for it to perform clean-up operations
StringBuffer class does not override equals method of the Object class, hence it performs a shallow
compare on Object references.
Variables cannot be declared STATIC within any method. This is because lifetime and scope of local
variables is restricted to that of the method in which they are declared whereas static variables are
class variables and they are loaded when the class is loaded, and exist in memory till the class is
unloaded.
Garbage collector thread is the least priority thread, and it does “NOT” ensure that the program will not
run out of memory.
e.g. : int I = 4;
int a[] [] [] = new int [I] [I=3][I] ;
is perfectly legal.
Writer classes (PrintWriter , FileWriter etc.) are more oriented towards Unicode characters
All method binding in Java uses late binding unless method unless the method is declared FINAL.
An Interface can have a static inner class in its namespace
In the derived class constructor one should only call private/final methods of base class.
All arrays in Java are objects. Each is associated with a class, which can be retrieved by getClass()
method.
An interface cannot have the same name as any of its enclosing class/interface.
Class Myclass
{
interface Myclass // Compiler error
{ ….. }
}
Non-static inner class cannot have static variables unless they are declared FINAL.
Map and Set cannot have duplicate elements.
Static block cannot make a forward reference to static variables defined after its definition.
A.equals(B) will return true if and only if the 2 objects A and B are of the same class.
Null literal does not have any type, hence it cannot be cast to any other type.
Blank final variables have to be initialized. If they are class members, they can be initialized only in
initializer blocks/constructors.
E.g.:
Class A
{
final int var;
{
var = 10;
} // Initializer block
}
e.g. :
class A
{
int a = 10;
}
class B extends A
{
static int a = super.a; // COMPILE ERROR
}
Method declaring a non-void return type in its header may not provide a return statement in its body
if it declares to throw an Exception.
e.g :
int method ()
{
throw new RuntimeException();
}
OR
Int method () throws Exception
{
throw new Exception();
}
are valid.
Lock acquired on the object by a synchronized method is released if the method throws an
Exception
If an instance initializer block throws a checked Exception during its execution, which it does not
catch, then the Exception must be declared in the throws clause of every constructor of the class.
e.g. :
class Test
{
{
if(true) throw new Exception();
}
Test () throws Exception
{}
Test (int a) throws Exception
{}
There can be no statement in a method body after a throw statement.
e.g. :
class NewClass
{
private void method () throws Exception
{
throw new Exception();
return; // COMPILE error Statement not reachable
}
}
e.g. :
class Myclass
{
Myclass ()
{
class Constclass ()
{}
}
}
A thread can be called daemon if and only if the creating thread is daemon.
e.g. :
class A
{
A ()
{
return;
}
e.g. :
private static void method ()
{
class Innclass
{}
}
is legal and Innclass is implicitly static.But it should NOT be explicitly prefixed with static
keyword.
Static local class can only access ‘STATIC’ members of enclosing class
An object is eligible for Garbage collection if the only references to the object are from other which
are also eligible for GC.
Thread object set to Null will not stop the thread from execution.
If a method declares to throw a checked Exception, it must be caught by the catch block of the
Exception or a superclass of the Exception. Peer class of Exception in the catch block does not
satisfy the requirement and will flag a compiler error.
In GridLayout constructor, both rows and columns cannot be simultaneously zero, else
RuntimeException is thrown.
Calling yield () in synchronized method or block does NOT relinquish the lock.???
e.g. :
int a[] = new int[10];
System.out.println (a[0]); //NullPointerException.
Final static variables can be initialized only in static initializer blocks or during assignment.
e.g. :
abstract class A
{
abstract void method () ;
}
class B extends A
{
public void method()
{
super.method(); // COMPILER ERROR
}
}
Void method()
{
final int var;
}
is perfectly legal