Prev Next

Java / Access Modifiers

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

Can a class be declared as private?
Posted on Apr 1, 2016 by Senthil Kumar.

A class cannot be declared private.

Can a class be declared protected?
Posted on Apr 1, 2016 by Senthil Kumar.

No. A class cannot be protected.

Can an interface be declared protected?
Posted on Apr 1, 2016 by Senthil Kumar.

No. An interface cannot be protected.

What are the applicable access specifier for a class/interface/abstract class?

It can be public or no access specified(default access).

Can we run a Java program without main method?

No. However until Java 7, we can execute by using static blocks. Prior to Java 7, the Jvm loads the class, execute static block before finding main method and invoke it. During this process, any code in the static block got executed.

// will not work in Java 7 and above.
public class Test {
    static {
        System.out.println("Static block executed.");
    }
}
«
»
Constructor

Comments & Discussions