Java / Modifiers
1. What is the problem with the below code?
package org.javatutorials.accessModifer.protected; Invalid package name. protected is a java keyword.
2. Does the order of public and static declaration is important in main() method?
No. It can be placed in any order except that the return type of any method must before the method name.
3. Types of modifiers in Java.
There are two types of modifiers in java. Access Modifiers, Non-access Modifiers.
4. Can a class be protected?
An outer class cannot be protected. However an inner class can be.
5. Can an inner class be declared private?
Yes. The inner class can be private.
6. Can a class be declared private?
No. The outer class cannot be private.
7. Different modifiers and its access levels.
Access Levels Modifier Class Package Subclass (different package) World public Y Y Y Y protected Y Y Y N no modifier Y Y N N private Y N N N
8. What are access modifiers?
Access modifiers restricts the visibility of a class/field/method or a constructor.
9. Types of access modifiers.
There are four access modifiers in Java. private....
10. What are non-access modifiers in java?
Non-access modifiers doea not alter the accessibility level of variables and methods, however it enables other properties. static This modifier make a member as a class variable or method....
11. Where protected is used?
A class level method and fields/variable can be declared as protected.
12. Can an Interface have protected members?
No. An interface cannot have protected methods or fields.
13. Explain the scope of a protected method.
A protected method is visible within the same package and also visible to subclasses of the class in any package.
14. How do I prevent my class to be not inherited.
Declare your class as final. A class declared as final can't be extended by any other class.
15. can an abstract class be final?
No. abstract class must be extended by another concrete class.
16. Example of final class from Java API.
java.lang.String, java.lang.Math
17. Can a class be declared as static?
No. A outer class cannot be declared static.
18. Why do we declare a method as static.
When the method has to be accessed even before the instantiating an object for the class, we should declare the method as static.
19. What are the constraints on a static method or a static block of code?
A static method should not refer to instance variables without an instance. cannot use this operator to refer the current instance.
20. How do I print "Hello world" even before main() is executed?
Print the statement inside a static block of code. Static blocks execute when the class gets loaded into the memory and even before the creation of an object....
21. Can we declare static members in a method?
Methods will have only local variables. Static variables are class level and cannot reside inside a method.
22. What are the other applicable modifiers for main() method?
A main() Method is static by default. It can also be declared final and synchronized.
23. What happens when a main method declared as synchronized?
When main() declared as synchronized, no two threads can access the main() at the same time.
24. Explain the final keyword in Java.
A final keyword in Java is used on a class, with a variable and with a method and it restricts any further modification . When you use final keyword with an entity (class or variable or method), it denotes that...
25. Explain strictfp keyword in Java.
The strictfp keyword is used to force the precision of floating point calculations (float or double) in Java conform to IEEE 754 standard, explicitly. Without using strictfp keyword, the floating point precision may vary depends on target platform's hardware and...
26. Explain the native keyword in Java?
The native keyword is applied to a Java method to indicate that the method is implemented in native code using JNI (Java Native Interface).
27. What is the default implementation of hashcode method?
Object hashcode is a native method that returns distinct integers for distinct objects by converting the internal address of the object into an integer, but this implementation technique is not required by the Java. The default implementation is JVM-specific .
28. When is it appropriate to use final blank variables?
A final property of a class must have value assigned before even the object is created. So the last place would be to assign value is constructor.
29. Modifiers that disallow overriding when applied.
private, static and final methods cannot be overridden .