Tuesday, 4 August 2015

Java Tutorial : What is a Class?

🚀 Want to Master Core Java & OOP Concepts?

Subscribe to Ram N Java for beginner-friendly coding tutorials and practical guides!

🔔 Subscribe to YouTube Channel

What is a Class in Java? Class Structure and Syntax Explained

In Object-Oriented Programming (OOP), the class serves as the fundamental building block of your application. In Java, everything revolves around classes and objects. A class is essentially a user-defined blueprint, prototype, or template from which individual objects are created, defining both the data an object can hold and the actions it can perform.

1. Understanding the Blueprint Concept

To understand what a class is, consider a real-world architectural blueprint:

  • The Blueprint vs. The House: An architect draws a blueprint of a house. The blueprint itself is not a physical house—you cannot live inside it. However, using that blueprint, you can build multiple real houses with specific colors and interiors.
  • Class vs. Object: In Java, the class represents that architectural blueprint, while each individual object created from it is an actual physical instance residing in memory.
  • Logical vs. Physical: A class is a logical construct that occupies no memory for state until instantiated as an object.

2. Java Class Syntax and Structure

Defining a class in Java follows a standardized structure using the class keyword:

  • Access Modifiers: Keywords like public, default, or protected determine where the class can be accessed.
  • The class Keyword: Signals to the Java compiler that a new class blueprint is being declared.
  • Class Identifier: By convention in Java, class names follow PascalCase (e.g., Student, BankAccount).
  • Class Body: Enclosed within curly braces { ... }, containing all members of the class.

3. Core Components Inside a Java Class

A typical Java class incorporates several key components that define its state and behavior:

  • Fields / State: Variables declared within the class that represent the properties or data held by each object (e.g., name, age, salary).
  • Methods / Behavior: Functions defined within the class that operate on the object's data to perform actions or business logic.
  • Constructors: Special blocks of code invoked when an object is instantiated to initialize its initial state.
  • Blocks & Nested Declarations: Initialization blocks and inner classes when modular encapsulation is needed.

4. Key Best Practices

When declaring classes in Java:

  • Follow the Single Responsibility Principle: design each class around a single cohesive purpose.
  • Encapsulate member fields by declaring them private and exposing access via public getter and setter methods.
  • Name your classes with descriptive nouns that clearly communicate what entity the class models.

📺 Explore More Java Tutorials

Continue expanding your Java and OOP knowledge with these related tutorials from our channel:


Click here to watch on YouTube:
https://www.youtube.com/watch?v=JN5WWXDvQdA&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge:

Java Tutorial : What is a Class?
Java Tutorial : What is a Class?

Car.java

/**
 * Car class is the blueprint from which individual car 
 * objects are created.
 */
public class Car
{
    private String brand = null;
    private String model = null;
    private String color = null;

    public String getBrand()
    {
        return brand;
    }

    public void setBrand(String brand)
    {
        this.brand = brand;
    }

    public String getModel()
    {
        return model;
    }

    public void setModel(String model)
    {
        this.model = model;
    }

    public String getColor()
    {
        return color;
    }

    public void setColor(String color)
    {
        this.color = color;
    }

}

CarDemo.java

public class CarDemo
{
    public static void main(String[] args)
    {

        /*
         * Create maruthiAltok10 car object and 
         * set the properties
         */
        Car maruthiAltok10 = new Car();

        maruthiAltok10.setBrand("Maruthi Alto");
        maruthiAltok10.setModel("k10");
        maruthiAltok10.setColor("Orange");

        displayCarInformation(maruthiAltok10);

        /*
         * Create swift car object and 
         * set the properties
         */
        Car swift = new Car();

        swift.setBrand("Swift");
        swift.setModel("ZDI");
        swift.setColor("Red");
        
        displayCarInformation(swift);

        /*
         * Create maruthiAlto800 car object and 
         * set the properties
         */
        Car maruthiAlto800 = new Car();

        maruthiAlto800.setBrand("Maruthi Alto");
        maruthiAlto800.setModel("800");
        maruthiAlto800.setColor("Blue");
        
        displayCarInformation(maruthiAlto800);

    }

    private static void displayCarInformation(Car car)
    {
        System.out.println("Car Brand : " + car.getBrand());
        System.out.println("Car Model : " + car.getModel());
        System.out.println("Car Color : " + car.getColor());
        System.out.println("--------------------------------");
    }

}

Output

Car Brand : Maruthi Alto
Car Model : k10
Car Color : Orange
--------------------------------
Car Brand : Swift
Car Model : ZDI
Car Color : Red
--------------------------------
Car Brand : Maruthi Alto
Car Model : 800
Car Color : Blue
--------------------------------

To Download CarDemoClassApp Project Click the below link:

https://sites.google.com/site/javaee4321/java/CarDemoClassApp.zip?attredirects=0&d=1

No comments:

Post a Comment

Tutorials