Chapter 4: Methods and Encapsulation Flashcards
Name the 4 access modifiers in order of least restrictiveness
public - proctected - default - private
Which of these optional modifiers will you need for the exam? static abstract syncronized native strictfp final
static final abstract
What are the rules regarding use of varargs in a method?
1) a vararg parameter must come last in a methods parameter list 2) you can only have one vararg parameter per method.
How do you call a method with a vararg parameter?
1) you can either pass in an array e.g method( new int[] {4,5}); 2) list the elements of the array and java will create it for you e.g method(4,5): 3) omit the values and java creates an array of size 0 e.g - method( )
How do you access a vararg parameter?
like you would an array e.g nums[0]
What does the default access modifier do?
only classes in the same package can access the member
What does the protected access modifier do?
only classes in the same package and subclasses in any other package can access the member
what does static mean?
something belonging to a class rather than to an instance of that class.
Would this method compile public int get2X(int x){ if(x>0) return 2*x;
No, you cannot return a value conditionally as you must return the type you’ve declared in the signature
What are the rules for a method with a void return type
1) it returns nothing 2) it can use the word return but with a semicolon at the end. eg return;
Are these valid method declarations? void add(int a, b) // 1 void add(int a, final int b) // 2 add (int a, int b) // 3
1) invalid parameter declaration for b. 2) valid, a parameter can be declared final as long as its value is not changed in the method. if this happens, compile error. 3) invalid - no return type
Does java allow methods to return a value of a different type than the one specified?
yes only on 3 occasions: 1) numeric promotion 2) Autoboxing/unboxing 3) With inheritance
Will this code compile? public int getVal(int x) { char ch = ‘a’; byte b = 0; if(x>0) return ch; else return b; }
yes, due to numeric promotion, a byte or char can be returned in the place of an int. if the return type is numeric, then the value that is returned is only allowed to be smaller than the type declared. e.g short, byte & char can be returned in an int method.
Will these lines of code compile? public int getVal(){ return new Short( (short) 10); // 1 return new Long(10); //2 }
1) compiles as return type can be a wrapper object of a smaller type than the return type. This wrapper object will be unboxed into a primitive short. 2) will not compile, Long cannot be converted to int
Will these lines of code compile when run separately? public Integer getval(){ byte b = 10; return 10; // 1 return b; // 2
1) compiles as autoboxing is legal when it comes to return type. int 10 will be boxed into an Integer object 2) will not compile, byte cannot be converted to Integer. The constructor to turn a byte prim into a wrapper only takes a string or a byte. e.g byte b = new Byte((byte) 10);
Will this compile? String getValue(){ return new Object();
This will not compile as an Object is not a String. A String is an object though.
Are the following legal in java? class Foo{ private static void baz(){ } static public final void boz(){ }
yes // the static keyword has to come before the return type however the order of the modifies doesn’t matter
What types can’t be declared static in java?
Top level classes, interfaces, enums or local variables can’t be declared static.
What are the different ways of accessing static members of a class from another class?
1) it is convention to access a static member by referencing the class name. 2) Java also allows static members to be accessed through an instance variable. 3) you can also access static members through an implicit reference to an instance e.g public class Order { public static double taxRate = 0.05; // static variable public Order(){}. // constructor public double checking() { // instance method return new Order().taxRate = 5.0; // implicit reference to constructor is accessing a static variable inside an instance variable }
Assume Birds is a class and fly() is a static method of the Birds class. what will this print? class TestClass{ public static void main(String[] args){ Birds eagles = null; eagles.fly(); } }
It will compile and run without any output. Accessing static members with a null instance reference doesn’t throw an exception as the compiler determines that fly() is a static member of class Birds and translates it to Birds.fly(); The compiler doesn’t care what eagles might point to at runtime
Is access to static members determined at runtime or at compile time and how?
At compile time by the compiler by checking the declared type of variable.