Maruthi Krishna has Published 1025 Articles

Can we call methods using this keyword in java?

Maruthi Krishna

Maruthi Krishna

Updated on 30-Jul-2019 22:30:26

The "this" keyword in Java is used as a reference to the current object, within an instance method or a constructor. Yes, you can call methods using it. But, you should call them only from instance methods (non-static).ExampleIn the following example, the Student class has a private variable name, with ... Read More

Is it possible to use this keyword in static context in java?

Maruthi Krishna

Maruthi Krishna

Updated on 30-Jul-2019 22:30:26

A static method or, block belongs to the class and these will be loaded into the memory along with the class. You can invoke static methods without creating an object. (using the class name as reference).Whereas "this" in Java acts as a reference to the current object. But static contexts(methods and ... Read More

Can we use private methods in an interface in Java 9?

Maruthi Krishna

Maruthi Krishna

Updated on 30-Jul-2019 22:30:26

Yes, you can use private methods in an interface since Java9.Exampleinterface MyInterface {    public abstract void demo();    public default void defaultMethod() {       privateMethod();       staticPrivateMethod();       System.out.println("This is a default method of the interface");    }    public static void staticMethod() ... Read More

When and where static blocks are executed in java?

Maruthi Krishna

Maruthi Krishna

Updated on 30-Jul-2019 22:30:26

A static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.Example Live Demopublic class MyClass {    static{       System.out.println("Hello this is a static ... Read More

Can a try block have multiple catch blocks in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 30-Jul-2019 22:30:26

Yes, you can have multiple catch blocks for a single try block.ExampleThe following Java program contains an array of numbers (which is displayed). from user it accepts two positions from this array and, divides the number in first position with the number in second position.While entering values −If you choose ... Read More

Advertisements