DEV Community

Vidya
Vidya

Posted on

What is Class in java?

Class
In Java, a class is a fundamental building block of object-oriented programming. It acts as a blueprint or template used to create objects. A class defines the properties (variables) and behaviors (methods) that an object of that class can have. Without classes, Java programs cannot be structured or organized effectively.

Rules of a Class in Java

  1. A class must be declared using the class keyword.

  2. The class name should start with an uppercase letter by Java naming conventions.

  3. A class name must be a valid identifier (no spaces, special characters, or starting with numbers).

  4. A Java file can contain multiple classes, but only one public class is allowed.

  5. The public class name must match the file name exactly.

  6. A class can contain variables, methods, constructors, blocks, and inner classes.

  7. Access modifiers (public, private, protected, default) control the visibility of the class and its members.

  8. A class can extend only one class (single inheritance) but can implement multiple interfaces.

  9. A class can be created without a main method, but execution starts only from the class that contains main().

  10. Objects of a class are created using the new keyword.

Top comments (0)