ch4 Flashcards
(95 cards)
Spot the invalid methods
final public void napi() {} static public void nap1() {} final static public void nap2() {} final static void nap3() {} void public nap4() {} int public nap5() {} public int static nap6() {}; public final void walk(){};
nap4, 5 and 6 are invalids
What are the elements that are absolutely required when declaring methods?
Method declaration -
Return type
Method name
Parameter list
Method body
What are the four access modifiers in java?
public
private
protected
default
what does public access modifier mean?
It can be called from any class
protected and classes in the other packages
What about private?
Only can be called from within the same class.
What about the protected class?
Can be called from classes in the SAME package or subclass.
protected = inheritance OR SAME package
What about default access modifier can be called from?
Can be called from the same class within the same package
What is default access also known as
package private
Make a method with default access?
void hell(){ }
default void hello(){}
This wont compile bc there is no default keyword
Can you name some optional specifiers?
static, abstract, final
Can you put the optional specifiers in any other?
you can specify them in any order
public void walk1() {} public final void walk2() {} public static final void walk3() {} public final static void walk4() {} public modifier void walk5() {} public void final walk6() {} final public void walk7() {}
Invalids are -
walk5()
walk6()
public String walk4() {return "";} public String walk2() {return null;} public int w() {return 9L;}
returns empty
returns null
compile error
Does the following compile?
public void walk1() { } public void walk2() { return; } public String walk3() { return ""; } public String walk4() { } public walk5() { } String walk6(int a) { if (a == 4) return ""; }
walk4 does not compile bc no return type
walk5 does not compile
walk6 does not compile -> if a is never 4 return wont return
public void walk1() { } public void walk2 { } public void walk3(int a) { } public void walk4(int a; int b) { } public void walk5(int a, int b) { }
public void walk1() { }
public void walk2 { } // DOES NOT COMPILE
public void walk3(int a) { }
public void walk4(int a; int b) { } // DOES NOT COMPILE
public void walk5(int a, int b) { }
Will the following compile?
public void walk1() { } public void walk2;
public void walk1() { }
public void walk2; // DOES NOT COMPILE
Where does vargs args parameter be at?
last one only
public void hello(int a, int ... num){}
How many vargs args can you use in a method?
Only 1
Where can you put vargs args?
Only method
Can you do this?
public void hello(String … ar)
{
String … anotherArray = ar;
}
Only in parameter method is ok
String ..anotherArray is illegal
What is the output?
public static void main(String[] args) { runner (new int[] {null,3}); } public static void runner(int ... i ) { System.out.println(i.length); }
DNC null pointer
What is the output?
public static void main(String[] args) { runner (new Integer[] {null,3}); } public static void runner(Integer ... i ) { System.out.println(i.length); }
2
What is the output?
public static void main(String[] args) { runner (new Integer[] {}); } public static void runner(Integer ... i ) { System.out.println(i.length); }
zero