Optional specifiers for methods
Can they be in any order?
Yes
static
abstract
final
default
synchronized
There can be multiple specifiers listed on a method but certain combinations are not allowed if it doesn’t make sense to the compiler
Can you declare multiple throws exception in method declaration
Yes
Separated by comma
Local variable modifiers?
There is only one e
final
Remember a final variable needs to be initialized during declaration or initialized once before it’s used elsewhere
Effectively final variable
It’s a local variable not declared as final but never modified with in its block after initialization
Optional specifiers for instance variable
final - must be initialized with each instance of the class. Unlike other instance variable these final instance variables do not get defaults . They are to be explicitly initialized during instance creation
volatile - instructs JVM that the value can be modified by other threads
transient - used to indicate that an instance variable should not be serialized
Rules for creating a method with varargs
1) method can have at most one vararg parameter
2) it should be the last parameter if declared. Only one is allowed
Static members
Static members can be called from another static method or an instance method
But a static method can not use or call an instance member
void walk(int… args){
print(“hello”);
}
walk();
Does that work?
Yes.
You do t have to pass any vararg parameters. Java will create an array of length zero
Protected member access
Check where the code is executed. The class where code executes needs to have access. It doesn’t matter if object has access or not. It matters only where it gets executed
Output?
public class Snake{
public static String name=“venky”;
}
Snake s=null;
print(s.name);
A) doesn’t compile
B) null
C) NullPointerException
D) Venky
D
You may think it should throw the exception. Since it is static variable you don’t need an object. You could write Snake.name
JVM knows the reference type of null object and uses static member as if it’s referenced through class name
Static imports
import static Java.util.Arrays.asList;
Please note if we have an instance/static method with same name declared as a member , the instance or static method takes precedence over an imported one.
Does below compile
import static Java.util.Arrays;
import static Java.util.Arrays.asList;
static import Java.util.Arrays.;
import static Java.util.Arrays.;
Arrays.asList(“one”);
Only 2 and 4 compiles
1 doesn’t compile since it’s a regular import and should not use keyword static
3 doesn’t t compile because static word comes before import which is wrong
5 doesn’t compile because class Arrays is not imported which is not required actually but if used it has to be imported.
Limits of autoboxing and numeric promotion
Long x=6;
Doesn’t compile. Java can not do both autoboxing and numeric promotion. Either it does promotion to long or autoboxing to Integer. Neither works for Long on left side
Unboxing and numeric promotion in one line
Integer a=18;
long x=a;
Compiles fine
Here unboxing and numeric promotion can be done at the same time [ where as autoboxing and numeric promotion both can’t be done at same time ]
Unboxing a null
Long x=null;
long y=x;
Compiles but throws an exception
Internally JVM does x.longValue()
Autoboxing of arrays
Long[] a={23,67};
Integer[] b={23,67};
Double[] c={12, 3};
1 doesn’t compile because it can’t do both autoboxing and numeric promotion
2 compiles because it needs to do only autoboxing
3 doesn’t compile because the data types not matching. Values are auto boxed to Integer which is not same as Double
Overloaded methods definition
Different method signature (name and parameter list)
They can have different combinations of access modifiers and optional specifiers and exceptions list
Order of preference for glide(1,2)
glide(int a, int b)
glide(long a, long b)
glide(Integer a, Integer b)
glide(int… a)
Note: glide(Long a, Long b) doesn’t work
What returns ?
int juggle(boolean a, boolean… b){
return b.length;
}
juggle(true);
juggle(true, true);
juggle(true,true,true);
juggle(true,{true,true});
juggle(true, new boolean[4]);
juggle(true, new boolean[]{true,true});
0
1
2
Doesn’t compile . Not valid array
4
2