ch4 Flashcards
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
public void walk1(int... nums) { } public void walk2(int start, int... nums) { } public void walk3(int... nums, int start) { } public void walk4(int... start, int... nums) { }
public void walk1(int… nums) { }
public void walk2(int start, int… nums) { }
public void walk3(int… nums, int start) { } // DOES NOT COMPILE
public void walk4(int… start, int… nums) { } // DOES NOT COMPILE
If it is private,
Can members in the same class access?
Member in another class in same package?
Member in superclass in diff package
Method/field in non superclass class in diff package
Yes
No
No
No
If it is public,
Can members in the same class access?
Memeber in another class in same package?
Member in superclass in diff package
Method/field in non superclass class in diff package
Yes to all
If it is default
Can members in the same class access?
Member in another class in same package?
Member in superclass in diff package
Method/field in non superclass class in diff package
yes
yes
no
no
If it is protected,
Can members in the same class access?
Member in another class in same package?
Member in superclass in diff package
Method/field in non superclass class in diff package
yes
yes
yes
no
what is the purpose of static method?
For utility or helper methods that don’t require any object state. Since there is no need
to access instance variables, having static methods eliminates the need for the caller to
instantiate the object just to call the method.
■ For state that is shared by all instances of a class, like a counter. All instances must
share the same state. Methods that merely use that state should be static as well.
static int count =0;
public static void main (String[] args) { Rope a = new Rope(); a.count = 3; Rope b = null; a.count = 9; System.out.println(count); System.out.println(b); System.out.println(++b.count); }
}
9
null
10
What is the output?
~~~
public class Birthday {
static int count = 1;
public static void main(String[] args) {
Birthday a = new Birthday();
a.count=4;
System.out.println(a.counter);
a.counter=69;
System.out.println(a.counter);
Birthday b = null;
System.out.println(b.counter);
}
~~~
0
69
69
Can you call a instance method or variable from a static method?
Absolutely not.
public class chapterOne { int count = 0; public static void main(String[] args) { chapterOne a = new chapterOne(); a.count=4; chapterOne b = new chapterOne(); a.count=5; chapterOne cc = null; cc.count = 29; System.out.println(cc.count); }
Null pointer exception
What is the output?
private static final Integer[] values = new Integer[] {3,4,5}; private static final String val = null; static final StringBuilder change = new StringBuilder("Change"); public static void main(String[] args) { values[0]=null; change.append("add"); System.out.println(values[0]); System.out.println(val); System.out.println(change);
null
String val cannot be changed;
StringBuilder is ok
You cannot do this
static final Integer change = new Integer(1);
public static void main(String[] args) {
change=3;
System.out.println(change);
}
remember this.
public class Counter {
private static int count;
public Counter() { count++; }
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
System.out.println(count);
}
}
3y
public class ReferenceTypes {
public void fly(String s) {
System.out.print(“string “);
}
public void fly(Object o) {
System.out.print(“object “);
}
public static void main(String[] args) {
ReferenceTypes r = new ReferenceTypes();
r.fly(“string”);
r.fly(true);
String
Object
r.fly(true), tries to find boolean if not found it will be Object.
r.fly(4), tries to find int, then Integer, if both not found, it will be Object.