Given:
And the code fragment:
Which code fragment can be inserted at line n1 to enable the code to print All the best?
Given:
And the code fragment:
Which code fragment can be inserted at line n1 to enable the code to print All the best?
To enable the code to print 'All the best,' the method 'writeMsg()' should be called within the 'readMsg()' method. In Java, methods within the same class can be directly invoked using their names. Since 'writeMsg()' is an instance method and not a static method, calling 'writeMsg()' directly within 'readMsg()' is the correct approach. Therefore, the correct code fragment to be inserted at line n1 is 'writeMsg();'.
B Is wrong. Would be correct if the readMsg was declared as static, but it isn't. Correct answer is C.
C is the correct answer because, a method can access another method within a class directly. writeMsg() is an instance method and not a static method. B is incorrect because Messenger.writeMsg() implies as if writeMsg() is a static method. In order B to be correct, keyword "static" should be explicitly declared in writeMsg() method.
B is NOT correct answer!
c is correct .
Se puede llamar el un método publico dentro de otro método dentro de la misma clase