Prev Next

Java / Modifiers

Could not find what you were looking for? send us the question and we would be happy to answer your question.

What is the problem with the below code?

package org.javatutorials.accessModifer.protected;

Invalid package name. protected is a java keyword.

Can an inner class be declared private?

Yes. The inner class can be private.

Different modifiers and its access levels.

Access Levels
ModifierClassPackageSubclass (different package)World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N

Does the order of public and static declaration is important in main() method?
Posted on Apr 1, 2016 by Senthil Kumar.

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.

Types of modifiers in Java.

There are two types of modifiers in java.

Access Modifiers,

Non-access Modifiers.

Can a class be declared private?

No. The outer class cannot be private. We will get a compile time error.

Can a class be protected?

An outer class cannot be protected.
However an inner class can be.

What are access modifiers?

Access modifiers restricts the visibility of a class/field/method or a constructor.

Types of access modifiers.

There are four access modifiers in Java.


Private members are visible only inside the class in which they are defined.


Protected members of a class are visible within the package but they can be inherited to sub classes outside the package.


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.

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.


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 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 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.


When a method is synchronized it can be accessed by only one thread at a time.

Where protected is used?

A class level method and fields/variable can be declared as protected.

Can an Interface have protected members?

No. An interface cannot have protected methods or fields.

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.

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.

can an abstract class be final?

No. abstract class must be extended by another concrete class.

Example of final class from Java API.

java.lang.String, java.lang.Math

Can a class be declared as static?

No. A outer class cannot be declared static.

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.

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.
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. 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.

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.

What are the other applicable modifiers for main() method?

A main() Method is static by default.

It can also be declared final and synchronized.

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.

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 the entity is complete and can not be modified further.

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 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.
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).

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.

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. This pattern is usually followed in Immutable design pattern.

Modifiers that disallow overriding when applied.

private, static and final methods cannot be overridden .

Data types

Comments & Discussions