DEV Community

Elayaraj C
Elayaraj C

Posted on

DAY 5 In Java create an object,

Step 1: Define a Class

A class is a blueprint for creating objects.

// Define a class named Car
class Car {
String brand;
int speed;

// Constructor
Car(String brand, int speed) {
    this.brand = brand;
    this.speed = speed;
}

// Method to display car details
void displayInfo() {
    System.out.println("Brand: " + brand + ", Speed: " + speed + " km/h");
}
Enter fullscreen mode Exit fullscreen mode

}

Step 2: Create an Object

Now, let's create an object from this class in the main method.

public class Main {
public static void main(String[] args) {
// Creating an object of Car
Car myCar = new Car("Toyota", 120);

    // Calling a method on the object
    myCar.displayInfo();
}
Enter fullscreen mode Exit fullscreen mode

}

Explanation:

Class Definition: The Car class has properties (brand, speed) and a method (displayInfo).

Object Creation: new Car("Toyota", 120) creates an object and assigns it to myCar.

Method Call: myCar.displayInfo(); prints the car details.
Enter fullscreen mode Exit fullscreen mode

1. Local Variable

A local variable is declared inside a method, constructor, or block and is only accessible within that scope. It cannot be accessed outside the method or block it is declared in.

Image description

2. Global Variable (Instance and Static Variables)

Java does not have global variables like in some other languages. However, instance variables (non-static fields) and static variables (class-level fields) serve a similar purpose.
Instance Variable (Non-Static Global Variable)

An instance variable is declared inside a class but outside a method. It belongs to an object, and each object has its own copy.

Image description

Rules of Creating Class:

  1. Syntax of a Java Class

class ClassName {
// Fields (Variables)
DataType variableName;

// Constructor
ClassName() {
    // Initialization code
}

// Methods (Functions)
void methodName() {
    // Method code
}
Enter fullscreen mode Exit fullscreen mode

}

  1. Rules for Creating a Class 1️⃣ Class Name Rules

✅ Valid:

Must start with a letter, $, or _

Can contain letters, digits (0-9), _, and $

Cannot be a Java keyword (like class, public, etc.)

Java convention: Class names should start with an uppercase letter.
Enter fullscreen mode Exit fullscreen mode

❌ Invalid:

class 123Car {} // ❌ ERROR: Cannot start with a number
class class {} // ❌ ERROR: "class" is a keyword
class my-car {} // ❌ ERROR: Cannot contain hyphens (-)

✅ Correct Example:

class Car {} // ✅ Correct (Capitalized)
class myCar {} // ✅ Correct (Camel case)
class $Vehicle {} // ✅ Valid (Uses $)
class _Bike {} // ✅ Valid (Uses _)

2️⃣ Class Declaration Rules

✅ Correct:

Use the class keyword to define a class.

The filename should match the class name if it is public.

A Java file can have multiple classes, but only one public class.
Enter fullscreen mode Exit fullscreen mode

❌ Invalid:

public class Example {}
class Another {}
// ❌ ERROR: The filename must be Example.java since it has a public class.

3️⃣ Access Modifiers for Classes

Image description
✅ Examples:

public class Animal {} // ✅ Can be accessed from anywhere
final class Car {} // ✅ Cannot be inherited
abstract class Shape {} // ✅ Must be extended, cannot create objects

4️⃣ Class Members (Fields and Methods)

Inside a class, you can define variables (fields) and methods (functions).

Fields and methods can have different access modifiers (public, private, protected).
Enter fullscreen mode Exit fullscreen mode

✅ Example:

class Person {
String name; // Instance variable
int age;

void display() {
    System.out.println("Name: " + name + ", Age: " + age);
}
Enter fullscreen mode Exit fullscreen mode

}

5️⃣ Constructors Rules

A constructor must have the same name as the class.

It cannot have a return type.

If no constructor is defined, Java provides a default constructor.
Enter fullscreen mode Exit fullscreen mode

✅ Example:

class Car {
String brand;

// Constructor
Car(String brandName) {
    brand = brandName;
}
Enter fullscreen mode Exit fullscreen mode

}

6️⃣ Inheritance Rules

A class can inherit another class using extends.

A class cannot inherit multiple classes (Java does not support multiple inheritance).
Enter fullscreen mode Exit fullscreen mode

✅ Example:

class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}

❌ Invalid Multiple Inheritance:

class A {}
class B {}

// ❌ ERROR: Java does not support multiple inheritance
class C extends A, B {}

7️⃣ Static and Non-Static Members

static members belong to the class, not instances.

non-static members belong to objects.
Enter fullscreen mode Exit fullscreen mode

✅ Example:

class Example {
static int count = 0; // Shared among all objects
int instanceVar; // Each object has its own copy
}

Image description
Example: Fully Functional Class

// File: Person.java
public class Person {
String name;
int age;

// Constructor
Person(String name, int age) {
    this.name = name;
    this.age = age;
}

// Method
void displayInfo() {
    System.out.println("Name: " + name + ", Age: " + age);
}

// Main method
public static void main(String[] args) {
    Person p1 = new Person("Alice", 25);
    p1.displayInfo();
}
Enter fullscreen mode Exit fullscreen mode

}

✅ Output:

Name: Alice, Age: 25

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed: Zero in on just the tests that failed in your previous run
  • 2:34 --only-changed: Test only the spec files you've modified in git
  • 4:27 --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • 5:15 --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • 5:51 --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Watch Full Video 📹️

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

If this article connected with you, consider tapping ❤️ or leaving a brief comment to share your thoughts!

Okay