Java program without making class

The idea is to us enum is Java. Every enum constant is always implicitly public static final. Since it is static, we can access it by using enum Name. Since it is final, we can’t create child enums.

We can declare main() method inside enum. Hence we can invoke enum directly from the Command Prompt.

// A Java program to demonstrate that we can have // main() inside enum class. enum Color RED, GREEN, BLUE; // Driver method public static void main(String[] args) Color c1 = Color.RED; System.out.println(c1);

Please note that enum also uses class internally. The purpose of this post is create an interesting question where user doesn’t have to create a class explicitly.

Raghawendra Singh Like Article -->

Please Login to comment.

Similar Reads

Decision Making in Java (if, if-else, switch, break, continue, jump)

Decision Making in programming is similar to decision-making in real life. In programming also face some situations where we want a certain block of code to be executed when some condition is fulfilled. A programming language uses control statements to control the flow of execution of a program based on certain conditions. These are used to cause t

7 min read Making API Calls using Volley Library in Android

Volley is an HTTP library that’s used for caching and making a network request in Android applications. It is an HTTP library that makes networking for Android apps easier and most importantly, faster. API stands for Application Programming Interface. It is a way for two or more computer programs to communicate with each other. By using its product

4 min read Java Program to Swap two Strings Without Using any Third Variable

Given two string variables, a and b, your task is to write a Java Program to swap these variables without using any temporary or third variable. Use of library methods is allowed. Examples: Input: a = "Hello" b = "World" Output: Strings before swap: a = Hello and b = World Strings after swap: a = World and b = Hello Method 1 : In order to swap two

4 min read Java.lang.Class class in Java | Set 1

Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It has no public constructor. Class objects are cons

15+ min read Java.lang.Class class in Java | Set 2

Java.lang.Class class in Java | Set 1 More methods: 1. int getModifiers() : This method returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and interface. These modifiers are already decoded in Mo

15+ min read Difference Between java.sql.Time, java.sql.Timestamp and java.sql.Date in Java

Across the software projects, we are using java.sql.Time, java.sql.Timestamp and java.sql.Date in many instances. Whenever the java application interacts with the database, we should use these instead of java.util.Date. The reason is JDBC i.e. java database connectivity uses these to identify SQL Date and Timestamp. Here let us see the differences

7 min read Program to check the validity of password without using regex.

Password checker program basically checks if a password is valid or not based on the password policies mention below: Password should not contain any space. Password should contain at least one digit(0-9). Password length should be between 8 to 15 characters. Password should contain at least one lowercase letter(a-z). Password should contain at lea

9 min read Execute main() multiple times without using any other function or condition or recursion in Java

Given task is to execute main() multiple times without using any other function and without recursion() and without error. Given condition is that if executing main() n times then you can only call him (n-1) times. Solution: class Test < // static block static < main(new String[] < "Hello" >); > public static void main(String[] args) < Sy

1 min read Print Hello World Without using a Semicolon in Java

Every statement in Java must end with a semicolon as per the basics. However, unlike other languages, almost all statements in Java can be treated as expressions. However, there are a few scenarios when we can write a running program without semicolons. If we place the statement inside an if/for statement with a blank pair of parentheses, we don’t

2 min read Creating Java Project Without Maven in Apache NetBeans (11 and Higher)

In this article, you will find the steps to create a simple Java Project without Maven in Apache NetBeans (v 11 and higher). Since maven has launched, the creation of Java Project directly has been shifted under the "Java with Ant", due to which the geeks who upgrade themselves to the new Apache NetBeans face problem finding the option to create a

3 min read Java - Calling Non Static Members Directly From Constructor Without Using the Object Name

Java is an Object-Oriented Programming(OOP) language. We need to create objects in order to access methods and variables inside a class. However, this is not always true. While discussing static keywords in java, we learned that static members are class level and can be accessed directly without any instance. Here we will be discussing how we can a

3 min read How to Execute Instance Initialization Block (IIB) without Creating Object in Java?

In a Java program, operations can be performed on methods, constructors, and initialization blocks. Instance Initialization Blocks or IIB are used to initialize instance variables. We know that the instance block is the name-less method in java inside which we can define logic and they possess certain characteristics. Instance block logic is common

3 min read Java.util.TimeZone Class (Set-2) | Example On TimeZone Class

TimeZone class (the methods of this class was discussed in this article Java.util.TimeZone Class | Set 1) can be used in many cases like using TimeZone class we can get the time difference in term of hour and minute between two places.Problem : How we can get time difference of time in terms of hours and minutes between two places of Earth?Solution

5 min read Implement Pair Class with Unit Class in Java using JavaTuples

To implement a Pair class with a Unit class in Java using JavaTuples, you can use the Pair class provided by the library and create a new Unit class that extends the Unit class provided by the library. Here is an example implementation: Here is an example Java code that uses the MyPair class with MyUnit and displays the output: Java Code import org

4 min read Implement Triplet Class with Pair Class in Java using JavaTuples

Following are the ways to implement Triplet Class with Pair Class Using direct values import java.util.*; import org.javatuples.*; class GfG < public static void main(String[] args) < // create Pair Pair<Integer, String> pair = new Pair<Integer, String>( Integer.valueOf(1), "GeeksforGeeks"); // Print the Pair System.out.printl

2 min read Implement Quintet Class with Quartet Class in Java using JavaTuples

Following are the ways to implement Quintet Class with Quartet Class Using direct values import java.util.*; import org.javatuples.*; class GfG < public static void main(String[] args) < // create Quartet Quartet<String, String, String, String> quartet = new Quartet<String, String, String, String>( "Quartet", "Triplet

4 min read Implement Quartet Class with Triplet Class in Java using JavaTuples

Following are the ways to implement Quartet Class with Triplet Class Using direct values import java.util.*; import org.javatuples.*; class GfG < public static void main(String[] args) < // create Triplet Triplet<String, String, String> triplet = new Triplet<String, String, String>( "Triplet 1", "1", "GeeksforGe

3 min read Implement Octet Class from Septet Class in Java using JavaTuples

Prerequisite: Octet Class, Septet Class Below are the methods to implement a Octet Class using Septet Class in Java: Using direct values // Java program to illustrate // implementing Octet Class // from Septet Class // using direct values import java.util.*; import org.javatuples.*; class GfG < public static void main(String[] args) < // Create Sep

6 min read Implement Ennead Class from Octet Class in Java using JavaTuples

Prerequisite: Ennead Class, Octet Class Below are the methods to implement a Ennead Class using Octet Class in Java: Using direct values // Java program to illustrate // implementing Ennead Class // from Octet Class // using direct values import java.util.*; import org.javatuples.*; class GfG < public static void main(String[] args) < // Create Oct

7 min read Implement Sextet Class from Quintet Class in Java using JavaTuples

Prerequisite: Sextet Class, Quintet Class Below are the methods to implement a Sextet Class using Quintet Class in Java: Using direct values // Java program to illustrate // implementing Sextet Class // from Quintet Class using // direct values import java.util.*; import org.javatuples.*; class GfG < public static void main(String[] args) < // crea

5 min read Implement Septet Class from Sextet Class in Java using JavaTuples

Prerequisite: Septet Class, Sextet Class Below are the methods to implement a Septet Class using Sextet Class in Java: Using direct values // Java program to illustrate // implementing Septet Class // from Sextet Class // using direct values import java.util.*; import org.javatuples.*; class GfG < public static void main(String[] args) < // Create

6 min read Implement Decade Class from Ennead Class in Java using JavaTuples

Prerequisite: Decade Class, Ennead Class Below are the methods to implement a Decade Class using Ennead Class in Java: Using direct values // Java program to illustrate // implementing Decade Class // from Ennead Class // using direct values import java.util.*; import org.javatuples.*; class GfG < public static void main(String[] args) < // Create

8 min read Difference between Abstract Class and Concrete Class in Java

Abstract Class: An abstract class is a type of class in Java that is declared by the abstract keyword. An abstract class cannot be instantiated directly, i.e. the object of such class cannot be created directly using the new keyword. An abstract class can be instantiated either by a concrete subclass or by defining all the abstract method along wit

5 min read Java - Inner Class vs Sub Class

Inner Class In Java, one can define a new class inside any other class. Such classes are known as Inner class. It is a non-static class, hence, it cannot define any static members in itself. Every instance has access to instance members of containing class. It is of three types: Nested Inner ClassMethod Local Inner ClassAnonymous Inner Class Genera

3 min read Java I/O Operation - Wrapper Class vs Primitive Class Variables

It is better to use the Primitive Class variable for the I/O operation unless there is a necessity of using the Wrapper Class. In this article, we can discuss briefly both wrapper class and primitive data type. A primitive data type focuses on variable values, without any additional methods.The Default value of Primitive class variables are given b

3 min read Using predefined class name as Class or Variable name in Java

In Java, you can use any valid identifier as a class or variable name. However, it is not recommended to use a predefined class name as a class or variable name in Java. The reason is that when you use a predefined class name as a class or variable name, you can potentially create confusion and make your code harder to read and understand. It may a

5 min read Why BufferedReader class takes less time for I/O operation than Scanner class in java

In Java, both BufferReader and Scanner classes can be used for the reading inputs, but they differ in the performance due to their underlying implementations. The BufferReader class can be generally faster than the Scanner class because of the way it handles the input and parsing. This article will guide these difference, provide the detailed expla

3 min read Java.io.ObjectInputStream Class in Java | Set 2

Java.io.ObjectInputStream Class in Java | Set 1 Note : Java codes mentioned in this article won't run on Online IDE as the file used in the code doesn't exists online. So, to verify the working of the codes, you can copy them to your System and can run it over there. More Methods of ObjectInputStream Class : defaultReadObject() : java.io.ObjectInpu

6 min read Java.lang.StrictMath class in Java | Set 2

Java.lang.StrictMath Class in Java | Set 1More methods of java.lang.StrictMath class 13. exp() : java.lang.StrictMath.exp(double arg) method returns the Euler’s number raised to the power of double argument. Important cases: Result is NaN, if argument is NaN.Result is +ve infinity, if the argument is +ve infinity.Result is +ve zero, if argument is

6 min read java.lang.instrument.ClassDefinition Class in Java

This class is used to bind together the supplied class and class file bytes in a single ClassDefinition object. These class provide methods to extract information about the type of class and class file bytes of an object. This class is a subclass of java.lang.Object class. Class declaration: public final class ClassDefinition extends ObjectConstruc