Abhishek Kurve Abhishek Kurve - 2 months ago 7
Java Question

Why does my program not progress to the next class?

The program shows the output for only "A" class which is 10.
But why does the program not advance to the class "B" whose answer should be 12?

interface ConstInterface {
int MAX=10;
}

public class A implements ConstInterface {

public static void main(String[] args) {
System.out.println(MAX);
}
}

class B {
int MAX=12;
void maxNew(){
System.out.println(MAX);
B obj = new B();
obj.maxNew();
}
}

Answer

Your program ends when main() returns. You have no code in main() that calls anything in class B. That's why the program "does not advance".

You could change this by moving the two lines

B obj = new B();
obj.maxNew();

out of the maxNew() method in B and putting them inside main():

public static void main(String[] args) {
    System.out.println(MAX);
    B obj = new B();
    obj.maxNew();
}
Comments