How to Create Custom Class in Java? Last Updated : 23 Jul, 2025 Suggest changes Share 5 Likes Like Report Class is the collection of objects. Class is not a real-world entity it is just only templates and prototypes or blueprints. Class does not occupy memory. We can write a custom class as per our choice for an illustration purpose a sample is shown in the program below as a helper class. Example: Java // Java Program to Creating our Own Custom Class // Importing input output classes import java.io.*; // Class 1 // Helper class class Employee { // Member variables of this class // first attribute int id; // second attribute int salary; // third attribute String name; // Member function of this class // Method 1 public void printDetails() { // Print and display commands System.out.println("My id is " + id); System.out.println("This is my name " + name); } // Method 2 public int getSalary() { // Simply returning the salary return salary; } } // Class 2 // Main class class Custom { // Main driver method public static void main(String[] args) { // Display message only System.out.println("This is the custom class"); // Creating object of custom class in the main() // method Instantiating a new Employee object Employee harry = new Employee(); // Again creating object of custom class and // instantiating a new Employee object Employee robin = new Employee(); // Initializing values for first object created // above harry.id = 23; harry.salary = 100000; harry.name = "Ritu bhatiya"; // Initializing values for second object created // above robin.id = 25; robin.salary = 150000; robin.name = "Amit thripathi"; // Printing object attributes by // calling the method as defined in our class harry.printDetails(); robin.printDetails(); // Calling the method again of our class and // storing it in a variable int salary = robin.getSalary(); // Print and display the above salary System.out.println("Salary of robin : " + salary + "$"); System.out.println("ID : " + harry.id); } } OutputThis is the custom class My id is 23 This is my name Ritu bhatiya My id is 25 This is my name Amit thripathi Salary of robin : 150000$ ID : 23 Create Quiz B bhatiyaritu1122 Follow 5 Article Tags : Java Java-Class and Object GBlog 2025 Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like