Develop Java Program Without Using User-defined Class

Can we develop a Java program without using a user-defined class?

We can’t develop a Java program without class, but we can develop a Java program without using a user-defined class.

To run a program there are many core classes are work internally. Every Java program internally uses many pre-defined classes. So, we can’t develop a Java program without using a pre-defined class.

But it is possible to write a Java program without using a user-defined class. If we use Enum or Interface then it will be extended from another class. Those classes are pre-defined classes, not user-defined classes.

Using Enum

From Java5 onwards we can use Enum to develop a program without using a user-defined class.

public enum UserDefinedEnum {
   a;
   public static void main(String[] args) {
     System.out.println("Know Program");
   }
}

The Output of this Java program:-
Know Program

The javap tool shows that internally UserDefinedEnum uses java.lang.Enum class. It is extended from java.lang.Enum class but that class (java.lang.Enum) is a pre-defined class, not a user-defined class. Our question was “can we develop a Java program without using a user-defined class?”. Here we are not using any user-defined class.

> javap UserDefinedEnum

Compiled from "UserDefinedEnum.java"
public final class UserDefinedEnum extends java.lang.Enum<UserDefinedEnum> {
  public static final UserDefinedEnum a;
  public static UserDefinedEnum[] values();
  public static UserDefinedEnum valueOf(java.lang.String);
  public static void main(java.lang.String[]);
  static {};
}

Every user-defined class is a subclass of Java.lang.Object class. So, internally every user-defined Java class uses the pre-defined class. To execute every Java program, java.lang.Object is loaded by the Java. If we are using println() method to display the output, The println() method is defined in the System class so, JVM also loads the System class. There are so many classes that are internally used by the JVM. Hence We can’t write a Java program without using a class but we can develop a Java class without using a user-defined class.

Develop Java Program Without Using User-defined Class using enum
Using Enum

Using an Interface

From Java SE 8.0 onwards in an interface, in addition to abstract methods, we are allowed to create a method with an implementation body, those methods are called default and static implementation methods. So, from Java SE 8.0 onwards we can use Interface to develop a Java program without using any class.

public interface UserDefinedInterface{
   public static void main(String[] args) {
     System.out.println("Know Program");
   }
}

Output:-

Know Program

> javap UserDefinedInterface.java

Compiled from "UserDefinedInterface.java"
public interface UserDefinedInterface {
  public static void main(java.lang.String[]);
}
Develop Java Program Without Using User-defined Class using interface
Using an Interface

The javap tool describes that directly, the UserDefinedInterface it is not using any user-defined or pre-defined class. But internally it uses many pre-defined classes. using javap -verbose UserDefinedInterface command we can get all information of the UserDefinedInterface program.

Also see:- Read and display .class file version

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *