How to Create a Java Class: Step-by-Step Guide

Creation of Java Class

A Java class is a blueprint or template used to create Java objects. It defines the properties (variables) and behaviors (methods) that the objects of the class will have.
The content defined inside a Java class is represented inside the Java object created from that class.

How to Create a Java Class

A Java class is created using the class keyword, followed by a valid identifier (class name).

Java Class Syntax:

class ClassName {
    // variables
    // methods
    // constructors
}

Important Points About Java Class

  • A Java class can contain:
  1. Variables
  2. Methods
  3. Constructors
  4. Blocks and nested classes
  • A Java class must be saved with the .java file extension.
  • The file name should match the class name when the class is declared as public.

Main Method in Java

  • For execution, a Java program must contain the main method.
  • The main method acts as the entry point of a Java program.
  • If the main method is not present in the class, the program will compile successfully, but during execution, the JVM generates a runtime error.

Main Method Syntax:

public static void main(String[] args) {
    // program execution starts here
}

Compiler vs JVM Behavior

  • The Java compiler checks only the syntax of the Java program.
  • The compiler does not check whether the main method is present.
  • The JVM checks for the main method at runtime.
  • If the main method is missing, the JVM throws a runtime error.

Comments

Popular posts from this blog

History of Java Programming Language | Features, Origin & Uses

Inheritance in Java: Types, Examples, and Explanation

Java Programming Features Every Beginner Should Know