Click here for Part 1 - SCJP 1.5 - Chapter 1(Part 1) - Declaration and Access Control - Notes
The final keyword:
- final methods cannot be over-ridden
- the value of final variables cannot be changed
- the value in final arguments cannot be changed inside the method
- if a reference variable is marked final, it cannot be re-assigned to another object(the data in the object can be modified, buth the reference variable cannot be made to refer to another object).
eg. public Contact getContact(int fileName, final int phoneNumber)
The value of the phoneNumber cannot be changed inside the method.
Abstract method:
- Methods are declared, but not implemented. The first concrete sub-class must implemet all the abstract methods of an abstract class
- If atleast one method is abstract, then the class should be declared as an abstract class. An abstract class need not have any abstract methods.
eg.
- An abstract class cannot be private, static or final
Native methods:
- Methods implemented in a platform-dependant code, like C.
- native keyword applies only to methods.
Var-args(Methods with variable arguments, a JDK1.5 feature):
- Arguments : passed, during the method invocation
- Parameters : recevied by methods(in the method signature)
- eg. void doStuff(int... x); // expects one to many int parameters
- only the last parameter can have var-args:
eg. void doStuffs(int... x, int... y); //illegal
Constructor Declarations:
- In Java, objects are constructed
- If we dont create a constructor explicity, the compiler will construct one for us
- A constructor will not have any return type
eg.
2 types of variables in Java
- Primitives : char, byte, short, int, long, float, double, boolean
- Reference Variables: to refer an object, can refer the declared type or its sub-type.
Local (Automatic/Stack/Method) Variables:
- before the local variable can be used, it has to be initialized with a value (else gives compilation error)
- unlike instance varaibles, local varaibles do not get default values.
Shadowing:
Declaring a local variable with the same name as an instance variable
Stack & Heap:
Local variables & Reference to object variables are stored in Stack & Objects are stored in the heap.
The this keyword refers to the object currently running.
Array Declarations:
The size of an array should never be included in its declaration.
eg.
Transient Variable cannot be serialized. They are skipped while Serializing.
Volatile Variables:
Volatile variables can be modified concurrently by multiple threads. The volatile modifier tells the JVM that a thread accessing the variable must always reconcile its own private copy of the variable with the master copy in memory.
Transient and Volatile keyword can be applied only to instance variables.
Enums:
- cannot be defined within a method
- should be public (or) default
The final keyword:
- final methods cannot be over-ridden
- the value of final variables cannot be changed
- the value in final arguments cannot be changed inside the method
- if a reference variable is marked final, it cannot be re-assigned to another object(the data in the object can be modified, buth the reference variable cannot be made to refer to another object).
eg. public Contact getContact(int fileName, final int phoneNumber)
The value of the phoneNumber cannot be changed inside the method.
Abstract method:
- Methods are declared, but not implemented. The first concrete sub-class must implemet all the abstract methods of an abstract class
- If atleast one method is abstract, then the class should be declared as an abstract class. An abstract class need not have any abstract methods.
eg.
public abstract class PhoneBook {
public abstract void viewContacts();
}- An abstract class cannot be private, static or final
Native methods:
- Methods implemented in a platform-dependant code, like C.
- native keyword applies only to methods.
Var-args(Methods with variable arguments, a JDK1.5 feature):
- Arguments : passed, during the method invocation
- Parameters : recevied by methods(in the method signature)
- eg. void doStuff(int... x); // expects one to many int parameters
- only the last parameter can have var-args:
eg. void doStuffs(int... x, int... y); //illegal
void doStuffs(int x, int... y); //legal
Constructor Declarations:
- In Java, objects are constructed
- If we dont create a constructor explicity, the compiler will construct one for us
- A constructor will not have any return type
eg.
public class Tester {
protected Tester(){} // a constructor
protected void Tester(){} //not a constructor, but a method, since it has a return type
}
Variable Declarations:2 types of variables in Java
- Primitives : char, byte, short, int, long, float, double, boolean
- Reference Variables: to refer an object, can refer the declared type or its sub-type.
Local (Automatic/Stack/Method) Variables:
- before the local variable can be used, it has to be initialized with a value (else gives compilation error)
- unlike instance varaibles, local varaibles do not get default values.
Shadowing:
Declaring a local variable with the same name as an instance variable
Stack & Heap:
Local variables & Reference to object variables are stored in Stack & Objects are stored in the heap.
The this keyword refers to the object currently running.
Array Declarations:
The size of an array should never be included in its declaration.
eg.
int[5] scores; //illegal
Transient Variables:Transient Variable cannot be serialized. They are skipped while Serializing.
Volatile Variables:
Volatile variables can be modified concurrently by multiple threads. The volatile modifier tells the JVM that a thread accessing the variable must always reconcile its own private copy of the variable with the master copy in memory.
Transient and Volatile keyword can be applied only to instance variables.
Enums:
- cannot be defined within a method
- should be public (or) default