DEV Community

Cover image for Java_class,Object
Ajay Raja
Ajay Raja

Posted on

Java_class,Object

  • Java is an object-oriented programming language.
  • Everything revolves around class and object.

What is a Class in Java?

  • To create a class, use the keyword class.
  • A class is a user-defined data type in Java that groups variables and methods together into a single unit.
  • A class acts as a blueprint that describes how objects should look and behave.
class Student {
    String name;
    int age;
    void study() {
        System.out.println(name + " is studying");
    }
}
Here:
name,age → variables
study() → method
Student → class
Enter fullscreen mode Exit fullscreen mode

What is an Object in Java?

  • In Java, an object is created from a class.
  • An object is a real-world entity created from a class that contains actual data and can use the methods defined in the class.
  • An instance of a class means an object created from that class.

Why “Instance” is Important?

  • It represents the class in memory.
  • Multiple instances can be created from one class.
  • Each instance can store different values.
public class Main {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.name = "Ajay";
        s1.age = 21;
        s1.study();
    }
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)