- Following are invalid declarations,
int a[3];
int a[] = new int[];
int a[][] = new int [][4];
int a[] = new int[2]{1,2};
- Remember these points,
int [] a1 ,a2 ,a3;
The above declaration symbolizes that a1, a2, a3 are dmensional arrays.
Where as,
int a1 [], a2 , a3 ;
The above declaration symbolizes that a1 is a one dimensional array where as a2 and a3 are simple integer numbers.
→ General rule is that the big brackets i.e ‘[ ]’ must appear after the type name like int , long etc or after the identifier name. If the ‘[]’ appears after the type name then all the identifiers after that type name will be arrays of the specified type where as if the ‘[]’ appears after the identifier name then it only qualifies as the array of the specified dimension.
→array of a primitive type is a class derived from Object class itself and it also implements Cloneable and java.io.serializable.
→ ‘[]’ cannot occur before the identifier name during declaration i.e
int a1,[]a2,a3;//causes error because ‘[]’ appears before a2 and not after a2.
- While equating array reference variables if the dimensions aren’t appropriate then compile time error is not caused but runtime error sets in.
- In a given file only one public class can exist and also the name of the public class and the file must be the same.
- protected , private access modifiers cannot be applied to top level classes.
- among the other modifiers that cannot be applied to a top level class are static , synchronized ,native etc but we can apply abstract , final , strictfp.
- An anonymous class is not local class i.e local class is always a named class. Furthur an anonymous class is always final.
- A final field can be assigned a value only once (not necessary to do the initialization in the declaration part itself ).
- Compiler error is caused if the final instance variables which are declared aren’t initialized by the end of each constructor.
- if the final variable is method local , then even if it isn’t initialized compiler won’t complain ,unless it is used in some expression.
- A regular inner class may be declared both abstract and private but not abstract and final.
- Field variables and methods can share the same name as that of the enclosing class.
- A nested class and the outer class can’t share the same name.
- But a nested class , method , field variable can share same name provided the names of the nested class and enclosing class are different.
- A top level class can be declared abstract even if it doesn’t contain any abstract methods. It can even extended one of concrete classes and can implement other interfaces.
- Return types :-consider
byte m1(){
final int i = 1;
return i ;
}
here the method compiles fine as the integer variable is marked final and it’s value is known at compile time.
byte m2(final int i){
return i ;
}
here though variable ‘i’ is marked final ,it’s value is not determined at compile time and hence it results in compile time error.