Prev Next

Java / Modifiers

1. Can a class be declared private?

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

Read full answer

2. Can an inner class be declared private?

Yes. The inner class can be private.

Read full answer

3. What is the problem with the below code?

package org.javatutorials.accessModifer.protected; Invalid package name. protected is a java keyword.

Read full answer

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

Read full answer

5. Can a class be protected?

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

Read full answer

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

Read full answer

7. Types of modifiers in Java.

There are two types of modifiers in java. Access Modifiers, Non-access Modifiers.

Read full answer

8. What are access modifiers?

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

Read full answer

9. Types of access modifiers.

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

Read full answer

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. If this modifier is not specified, then those variable or method become instance members. final Final modifi...

Read full answer

11. Where protected is used?

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

Read full answer

12. Can an Interface have protected members?

No. An interface cannot have protected methods or fields.

Read full answer

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.

Read full answer

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.

Read full answer

15. can an abstract class be final?

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

Read full answer

16. Example of final class from Java API.

java.lang.String, java.lang.Math

Read full answer

17. Can a class be declared as static?

No. A outer class cannot be declared static.

Read full answer

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.

Read full answer

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.

Read full answer

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

Read full answer

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.

Read full answer

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.

Read full answer

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.

Read full answer

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

Read full answer

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 CPU's floating point processing ca...

Read full answer

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

Read full answer

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 .

Read full answer

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

Read full answer

29. Modifiers that disallow overriding when applied.

private, static and final methods cannot be overridden .

Read full answer

«
»

Comments & Discussions