Misc
From Suhrid.net Wiki
Revision as of 03:08, 26 August 2011 by Suhridk (talk | contribs) (moved Declaring and Initializing to Misc)
Legal Identifiers
- Can be composed of characters, numbers, currency-symbols and connecting chars such as underscore.
- Definition of characters are interpreted according to the charset.
- NO special chars such as #, :, ;, @, -, etc...
- Must start with letter, currency char, underscore but CANNOT start with a digit !
- Java identifiers are case-sensitive.
Illegal identifiers:
- int 23age;
- String em@iladd;
- List all-accounts;
These are legal!:
- int __;
- String $;
Final, Transient, Volatile
Final
- Variables marked as final and once initialized, cannot be reinitialized.
- Marking primitives as final is equivalent to making them a constant.
- For object references, final only prevents the reference from being assigned to another object.
- The object however can still be modified.
- There are no final objects, only final references.
- A final variable can only be initialized once, either via an initializer or an assignment statement. If a final instance variable is not assigned a value - there will be a compiler error !
- It need not be initialized at the point of declaration: this is called a 'blank final' variable.
- A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared or an instance initializer block can be used.
- Similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared.
- Instance Init Blocks or constructors cannot assign values to final static variables - this will be a compiler error.
- A local final variable must be initialized before it is used. If the variable is never used (blank final) then it wont cause a compiler error.
Transient
- Applies only to instance variables.
- Tells the JVM to skip this variable when serializing the object it is part of.
Volatile
- Applies only to instance variables.
- Tells the JVM that a thread accessing the variable must synchronize its private cache copy of the variable with the master copy in main memory.
- This is rarely used. Synchronization is used, rather than volatile to keep data thread-safe.
Local variables
- No access modifier is applicable to local variables.
- The only modifier that can be applied to local variables is final.
- Local variables are not automatically initialized, unlike instance and static variables.
- Compiler will complain when uninitialized local variable is first USED. If you don't try to use an uninitialized variable, it will be OK.
- Compiler can however detect in very simple conditional cases, that a local variable will be initialized, but will complain in complex cases.
e.g.
private void foo() {
int i;
System.out.println("i : " + i); //Compiler error
if(true) {
i = 10;
} else {
i = 20;
}
System.out.println("i : " + i); //OK. Compiler can figure out i will be definitely initialized.
int j;
boolean b = true;
if(b) {
j = 10;
}
System.out.println("j : " + j); //Compiler error - can't figure out if j will be initialized or not.
}
Shadowing
- The more local variable will always mask the instance level variable with the same name.
- "this" can be used to qualify the variable.
class Bar {
private int i = 10;
public void go() {
foo(20);
}
private void foo(int i) {
System.out.println(i); //local i - shadows instance i. Will print 20;
System.out.println(this.i); //Refers to instance i, will print 10;
}
}