Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Collections Class in Java
Next article icon

How to Create Custom Class in Java?

Last Updated : 20 Jul, 2021
Comments
Improve
Suggest changes
Like Article
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);     } } 

 
 


Output
This 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


 


Next Article
Collections Class in Java

B

bhatiyaritu1122
Improve
Article Tags :
  • Java
  • Java-Class and Object
  • GBlog 2025
Practice Tags :
  • Java
  • Java-Class and Object

Similar Reads

  • How to Import Custom Class in Java?
    Java language is one of the most popular languages among all programming languages. There are several advantages of using the java programming language, whether for security purposes or building large distribution projects. One of the advantages of using Java is that it tries to connect every concep
    3 min read
  • How to Execute a .class File in Java?
    A Java Class file is a compiled java file. It is compiled by the Java compiler into bytecode to be executed by the Java Virtual Machine. Step #1: Compile the .java File Open Terminal (Mac) or Command Prompt (Windows). Navigate to the folder containing the java file and type the following command to
    1 min read
  • How to create a Class in JShell of Java 9
    JShell is an interactive Java Shell tool, it allows us to execute Java code from the shell and shows output immediately. JShell is a REPL (Read Evaluate Print Loop) tool and runs from the command line. Jshell have the facility to create a class by which all the efforts can be reduced to write a whol
    2 min read
  • Java | How to create your own Helper Class?
    In Java, a Helper class is a class that contains useful methods which make common tasks easier, like error handling, checking input, etc. This class intends to give a quick implementation of basic functions so that the programmers do not have to implement them again and again. This class is easy to
    7 min read
  • Collections Class in Java
    Collections class in Java is one of the utility classes in Java Collections Framework. The java.util package contains the Collections class in Java. Java Collections class is used with the static methods that operate on the collections or return the collection. All the methods of this class throw th
    13 min read
  • How to Create Classes in Android Studio?
    In Android during the project development most of the time there is a need for classes in the projects. For example, in the case of CRUD operation, we need a model class to insert and retrieve data. Also to hold the info in our custom view we need to create a getter setter class. So basically in and
    3 min read
  • How to Create Your Own Annotations in Java?
    Annotations are a form of metadata that provide information about the program but are not a part of the program itself. An Annotation does not affect the operation of the code they Annotate.  Now let us go through different types of java annotations present that are listed as follows: Predefined ann
    5 min read
  • Component Class in Java
    The Component class is the superclass of all components. A component class can be linked with a page, components of web applications. Component clearly shows that is the graphical representation of an Object. Important methods of Component Class: public void add(Component c): This method inserts a c
    9 min read
  • CLASSPATH in Java
    Package in Java is a mechanism to encapsulate a group of classes, sub-packages, and interfaces. Packages are used for: Preventing naming conflicts. For example, there can be two classes with the name Employee in two packages, college.staff.cse.Employee and college.staff.ee.EmployeeMaking searching/l
    3 min read
  • How to Create Different Packages For Different Classes in Java?
    Let us first know what is a class and package in Java. Class in java is a model for creating objects. It means that the properties and actions of the objects are written in class. Properties are represented by variables and actions of the objects are represented by methods. So, a class contains vari
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences