Questions And Answers

More Tutorials

Java Class

Writing a Custom Class :

Syntax of a custom class :

class <class_name>{  

Example :
public class Employee {
	int id;			// Attribute 1
	String name;		// Attribute 2

Note: The first letter of a class should always be capital.

Any real-world object          = Properties + Behavior
Object in OOPs                 = Attributes + Methods

A Class with Methods :

We can add methods to our class Employee as follows:
public class Employee {
	public int id;
	public String name;

	public int getSalary(){
	public void getDetails(){



static class Employee{
    int id;
    int salary;
    String name;
    public void printDetails(){
        System.out.println("My id is " + id);
        System.out.println("and my name is "+ name);

    public int getSalary(){
        return salary;

public class custom_class {
    public static void main(String[] args) {
        System.out.println("This is our custom class");
        Employee harry = new Employee(); // Instantiating a new Employee Object
        Employee john = new Employee(); // Instantiating a new Employee Object

        // Setting Attributes for Harry = 12;
        harry.salary = 34; = "CodeWithHarry";

        // Setting Attributes for John = 17;
        john.salary = 12; = "John Khandelwal";

        // Printing the Attributes
        int salary = john.getSalary();


This is our custom class
My id is 12
and my name is CodeWithHarry
My id is 17
and my name is John Khandelwal


In this page (written and validated by ) you learned about Java Class . What's Next? If you are interested in completing Java tutorial, your next topic will be learning about: Java Access modifiers getters and setters in Java.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.