Two Most Important Questions asked in Java Interviews

Abhinay Gupta
3 min readJun 9, 2023

These two questions have been asked a lot of times to me in interviews:

  1. Why there is only single public class per source file?

2. Why non static method cannot be called from a static method?

1. Why there is only single public class per source file?

In Java, there can only be one public class per source file, and it must have the same name as the source file. This restriction is enforced by the Java language specification and is designed to ensure consistency and clarity in code organization. Here are a few reasons why this restriction exists:

1. Accessibility and Visibility: The access modifiers (`public`, `private`, etc.) control the visibility of classes, methods, and fields. When a class is declared as `public`, it means it can be accessed from other classes and packages. Having multiple `public` classes in a single file would create ambiguity in terms of which class is intended to be accessed publicly.

2. Compilation and Naming Conventions: Java source files follow a convention where the file name matches the name of the public class defined within it. This convention simplifies the compilation process and helps the compiler locate the appropriate class definition. If there were multiple public classes in a single file, it would be unclear which class should match the file name.

3. Code Organization and Readability: Limiting a source file to one public class promotes better code organization and improves code readability. It encourages developers to create separate files for each class, making it easier to locate and understand the structure of a codebase. It also aligns with the principle of having a single responsibility for each class.

However, it’s worth noting that a Java source file can contain multiple non-public classes or interfaces. These classes can be used internally within the file or by other classes in the same package, but they cannot be accessed from outside the package.

To summarize, the restriction of having only one public class per source file in Java helps maintain code organization, clarity, and accessibility. It ensures consistent naming conventions, simplifies compilation, and promotes good coding practices.

2. Why non static method cannot be called from a static method?

In Java, non-static methods belong to specific instances of a class, whereas static methods belong to the class itself. When a method is declared as static, it means it can be called without creating an instance of the class. On the other hand, non-static methods require an object of the class to be instantiated before they can be invoked.

The reason why non-static methods cannot be directly called inside a static method is that a static method does not have access to instance-specific data or methods. Static methods are not associated with any particular instance of the class and, therefore, do not have access to the instance variables or non-static methods.

When a static method is called, there is no implicit instance available, and it operates on the class level. It cannot refer to instance variables or use non-static methods because those are tied to specific instances of the class.

To access a non-static method within a static method, you need to create an instance of the class and invoke the non-static method on that instance. This ensures that the non-static method has access to the necessary instance variables and can operate on them.

Here’s an example to illustrate this concept:

public class MyClass {
private int value;

public static void staticMethod() {
// Cannot call non-static method directly from a static method
// valueMethod(); // This will result in a compilation error

// Create an instance and call the non-static method
MyClass instance = new MyClass();
instance.nonStaticMethod();
}

public void nonStaticMethod() {
// Access instance-specific data or perform operations
System.out.println("Value: " + value);
}
}

In the example, the `nonStaticMethod()` is a non-static method that accesses the `value` instance variable. To call `nonStaticMethod()` from `staticMethod()`, an instance of the class `MyClass` is created, and the non-static method is invoked on that instance.

By enforcing this restriction, Java ensures that the behavior of static methods remains consistent across different instances of the class and avoids any ambiguity in accessing instance-specific data or methods.

--

--