How to Create Custom Class in Java?
Last Updated :
20 Jul, 2021
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
import java.io.*;
class Employee {
int id;
int salary;
String name;
public void printDetails()
{
System.out.println( "My id is " + id);
System.out.println( "This is my name " + name);
}
public int getSalary()
{
return salary;
}
}
class Custom {
public static void main(String[] args)
{
System.out.println( "This is the custom class" );
Employee harry = new Employee();
Employee robin = new Employee();
harry.id = 23 ;
harry.salary = 100000 ;
harry.name = "Ritu bhatiya" ;
robin.id = 25 ;
robin.salary = 150000 ;
robin.name = "Amit thripathi" ;
harry.printDetails();
robin.printDetails();
int salary = robin.getSalary();
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
Share your thoughts in the comments
Please Login to comment...