Java Class and Object Programs
- Java program to print message using class
- Java program to find area and perimeter of a circle using class
- Java program to count digits of a number using class
- java program to find sum and product of all digits of a number using class
- Java program to check whether entered number is palindrome or not using a class
- Java program to check Armstrong number
- Java program to read and add two distances using class
- Java program to create members with private access modifier
- Java program to create members with access modifier
- Java program to create a singleton class
- Java program to create instances of singleton class and print hash codes
- Java program to compare instances of singleton class
- Java program to create a singleton class with method name as that of class
- Java program to implement cascaded method call
- Java program to create a method returning an object
- Java program to pass an object as an argument
- Java program to create an array of objects
- Java program to implement default or no-argument constructor
- Java program to implement constructor overloading
- Java program to implement a copy constructor
- Java program to implement constructor chaining
- Java program to create an object of a class as a data member in another class
- Java program to create an object of a class as a data member in another class
- Java program to demonstrate the instanceof operator
- Java program to create an anonymous object
- Java program to call a method using the anonymous object
- Java program to implement cascaded method call with an anonymous object
- Java program to check anonymous class using isAnonymousClass() method
- Java program to check whether the specified Class object represents an array or not
- Java program to check whether the specified Class object represents a primitive type or not
- Java program to check whether the specified Class object represents an interface type or not
- Java program to check whether an object belongs to a particular class or not
- Java program to demonstrate the newInstance() method
- Java program to get the class name using the object of the class
- Java program to represent the class in different ways
- Java program to call static block using the anonymous object
- Java program to check whether a class is a local class or not
- Java program to check whether a class is a member class or not
- Java program to check whether a class is declared as an enum or not
- Java program to check an annotation type using the isAnnotation() method
- Java program to demonstrate the getName() method
- Java program to get the simple name of the class
- Java program to print the class loader of the given class
- Java program to get the name of the superclass
- Java program to get the name of the generic superclass
- Java program to get the list of implemented interfaces in a class
- Java program to get the package name of a class
- Java program to get the list of fields of a class
- Java program to get the list of defined classes and interfaces in a class
- Java program to get the list of methods of a class
Program/Source Code:
The source code to create the singleton class is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we created a singleton class Singleton and public class Main. The Singleton class contains constructor and two methods getSingletonInstance(), SayHello(). The getSingletonInstance() method returns instance of class. The SayHello() method prints "Hello world" message.
The Main class contains a static method main(). The main() is an entry point for the program. Here, we created the object of the Singleton class and called SayHello() method.