Java / Modifiers
package org.javatutorials.accessModifer.protected;
Invalid package name. protected is a java keyword.
Yes. The inner class can be private.
Modifier | Class | Package | Subclass | World |
public | Y | Y | Y | Y |
protected | Y | Y | Y | N |
no modifier | Y | Y | N | N |
private | Y | N | N | N |
No. It can be placed in any order except that the return type of any method must before the method name. In this case, void has to specified before the main method name.
There are two types of modifiers in java.
Access Modifiers,
Non-access Modifiers.
No. The outer class cannot be private. We will get a compile time error.
An outer class cannot be protected.
However an inner class can be.
Access modifiers restricts the visibility of a class/field/method or a constructor.
There are four access modifiers in Java.
private.
Private members are visible only inside the class in which they are defined.
protected.
Protected members of a class are visible within the package but they can be inherited to sub classes outside the package.
public.
public members are visible universally.
default or No-access modifier.
Members of a class which are defined with no access modifiers are visible within the package in which they are defined.
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. If this modifier is not specified, then those variable or method become instance members.
final
Final modifier is used to declare a field as final i.e. it prevents its content from being modified. Final field must be initialized when it is declared.
A class can also be declared as final. A class declared as final cannot be inherited.
Method declared as final cannot be overridden.
abstract
Abstract classes contain abstract methods (you can refer them as ideas) so that they can be implemented in sub classes according to their requirements.
Abstract methods are incomplete or abstract without definition and the sub-class needs to provide the implmentation.
synchronized
When a method is synchronized it can be accessed by only one thread at a time.
A class level method and fields/variable can be declared as protected.
No. An interface cannot have protected methods or fields.
A protected method is visible within the same package and also visible to subclasses of the class in any package.
Declare your class as final. A class declared as final can't be extended by any other class.
No. abstract class must be extended by another concrete class.
java.lang.String, java.lang.Math
No. A outer class cannot be declared static.
When the method has to be accessed even before the instantiating an object for the class, we should declare the method as static.
- A static method should not refer to instance variables without an instance.
- cannot use this operator to refer the current instance.
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. Hence it will be executed before the main() method and it will be executed only once.
One class can have multiple static blocks and it executes sequentially.
Methods will have only local variables.
Static variables are class level and cannot reside inside a method.
A main() Method is static by default.
It can also be declared final and synchronized.
When main() declared as synchronized, no two threads can access the main() at the same time.
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 the entity is complete and can not be modified further.
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 CPU's floating point processing capability. strictfp restricts floating-point calculations and ensuring same result on every platform while performing operations in the floating-point variable.
- The strictfp keyword can be applied for classes, interfaces and methods.
- strictfp keyword cannot be applied on abstract methods, variables or constructors.
- strictfp cannot be applied on interface methods and abstract classes.
- If an interface or class is declared with strictfp, then all methods and nested types within that interface or class are implicitly strictfp.
The native keyword is applied to a Java method to indicate that the method is implemented in native code using JNI (Java Native Interface).
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.
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. This pattern is usually followed in Immutable design pattern.
private, static and final methods cannot be overridden .