Java Articles

Page 71 of 450

Does main() method accept arguments other than string array, in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 5K+ Views

The public static void main() method accepts an array of values of the type String Java from the user.public class{    public static void main(String[] args){    } }You can pass them at the time of execution right after the class name separated with spaces as −java ClassName 10 20 30And, in the program (from the main method) you can extract these values from the String array and use.ExampleFor example, you can use command line arguments to pass a and b in the above program as −public class Sample {    public static void main(String[] args){       int ...

Read More

Can we change return type of main() method in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 3K+ Views

The public static void main() method is the entry point of the Java program. Whenever you execute a program in Java, the JVM searches for the main method and starts executing from it.You can write the main method in your program with return type other than void, the program gets compiled without compilation errors. But, at the time of execution JVM does not consider this new method (with return type other than void) as the entry point of the program.It searches for the main method which is public, static, with return type void, and a String array as an argument.public static int ...

Read More

What are defender methods or virtual methods in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 784 Views

The default methods in an interface in Java are also known as defender methods or, virtual methods.The defender/virtual methods are those that will have a default implementation in an interface. You can define defender/virtual methods using the default keyword as −default void display() {    System.out.println("This is a default method"); }There is no need to implement these defender/virtual methods in the implementing classes you can call them directly.If you have an interface which is implemented by some classes and if you want to add a new method int it.Then, you need to implement this newly added method in all the ...

Read More

Can interfaces have Static methods in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 8K+ Views

An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.A static method is declared using the static keyword and it will be loaded into the memory along with the class. You can access static methods using class name without instantiation.Static methods in an interface since java8Since Java8 you can have static methods in an interface (with body). You need to call them using the name of the interface, just like static methods of a class.ExampleIn the following example, we are defining a static method in an interface and accessing ...

Read More

Can we change an exception of a method with throws clause from unchecked to checked while overriding it in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 635 Views

A checked exception is an exception that occurs at the compile time, these are also called as compile time exceptions. These exceptions cannot simply be ignored at the time of compilation; the programmer should take care of (handle) these exceptions.An unchecked exception is an exception that occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.Unchecked to checkedWhen a method in superclass throws an unchecked exception the method the subclass method overriding cannot throw ...

Read More

Can we create an object for an interface in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 17K+ Views

No, you cannot instantiate an interface. Generally, it contains abstract methods (except default and static methods introduced in Java8), which are incomplete.Still if you try to instantiate an interface, a compile time error will be generated saying “MyInterface is abstract; cannot be instantiated”.In the following example we an interface with name MyInterface and a class with name InterfaceExample.In the interface we have an integer filed (public, static and, final) num and abstract method demo().From the class we are trying to − create an object of the interface and print the num value.Exampleinterface MyInterface{    public static final int num = ...

Read More

Can abstract method declaration include throws clause in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 2K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();Yes, you can throw and exception from an abstract class.ExampleIn the following Java program, we have a two classes: one abstract class with name MyClass which have an abstract method (display()) and, another class that extends the abstract class.Here, the display() method abstract class throws an IOException.Exampleimport java.io.IOException; abstract class MyClass {    public abstract void display() throws IOException; } public class AbstractClassExample extends MyClass{    public void display() throws IOException { ...

Read More

Can the abstract methods of an interface throw an exception in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 2K+ Views

Yes, the abstract methods of an interface can throw an exception.ExampleIn the following example the interface (MyInterface) contains an abstract method with name display, which throws an IOException.import java.io.IOException; abstract interface MyInterface {    public abstract void display()throws IOException ; }Rules to be followedYou need to follow the rules given below while implementing such method −If the abstract method in the interface throws certain exception. The implemented method can throw the same exception as shown below −Example 1import java.io.IOException; abstract interface MyInterface {    public abstract void display()throws IOException ; } public class InterfaceExample implements MyInterface{    public void display()throws ...

Read More

What happens if the subclass does not override abstract methods in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 6K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation to it.A class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.Extending an abstract classOnce you extend an abstract class in Java you need to override all the abstractmethods in it or, declare it abstract. If you don’t, a ...

Read More

How to write a class inside an interface in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 401 Views

Defining a class within an interface is allowed in Java. If the methods of an interface accept a class as an argument and the class is not used elsewhere, in such cases we can define a class inside an interface.ExampleIn the following example we have an interface with name CarRentalServices and this interface has two methods that accepts an object of the class Car as an argument. Within this interface we have the class Car.interface CarRentalServices {    void lendCar(Car c);    void collectCar(Car c);    public class Car{       int carId;       String carModel;   ...

Read More
Showing 701–710 of 4,498 articles
« Prev 1 69 70 71 72 73 450 Next »
Advertisements