In this tutorials we are going to learn what is a Java class , rules to create a Java class and a simple java class example.

Java Class :

“A class is a way of binding the data and associated methods in a single unit.”

Any  JAVA  program  if we want  to  develop  then  that  should  be  developed with  respective class only i.e., without class   there is no JAVA program.

In object oriented programming, generally we can write two types of methods. Those are member methods and non-member methods.

  • A member method  is one which  is comes under the scope of the class. In Java we have only member methods. i.e we can not create a method with out class.
  • Non-Member methods are those which are not comes under the scope of a class. In Java there is no non-member methods.

Java Class Syntax :

[box type=”shadow” align=”alignleft” class=”” width=”100%”]

class <ClassName>{

Variables variable;

Methods methods;

}

[/box]

Here, class is a keyword which is used for developing or creating user defined datatypes. <ClassName>represents a  JAVA valid class name and  it  is  treated as name of the  class. Class names are used for creating obj.

A typical Java Class contains two parts namely variable declaration and method definitions.

Variable declaration  represents what  type  of data members which we use as a part of the class.

Method definition represents the type of methods, which are used to perform operations inside the class.

Access Levels Of a Class :

We can use 2 types of access modifies in front of a Java class. Those are public and default.

  • If we declare a class as public, the class can be accessed across all the packages in java application.
  • If we declare a class as default, the class can be accessed only with in that package.

Java Class Example :

[sourcecode language=”java”]

public class Student {
    private int studenttId;
    private String studentName;

    public int getStudenttId() {
        return studenttId;
    }

    public void setStudenttId(int studenttId) {
        this.studenttId = studenttId;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
}

[/sourcecode]

On the above example, we have created a Student class with public access modifier. This class can be accessed across the application.

Members of a Java Class :

A typical Java class contains data members (variables) and member functions (methods).

Data members : Data members means variables. If we take the above Student class, the data members are studentId and studentName.

The possible access modifiers of the data members are public, private, protected and default. You can get more information on Java access modifiers here.

The default access modifier of a data member in a class is default.

Member Functions : Member functions means methods. If we take the above student class, the member functions are get and setStudentId(), get and setSuudentName().

The possible access modifiers of the member functions are public, private, protected and default. You can get more information on Java access modifiers here.

Happy Learning 🙂