[go: up one dir, main page]

0% found this document useful (0 votes)
6 views3 pages

Java Is A High2

Uploaded by

hjkfdbns28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Java Is A High2

Uploaded by

hjkfdbns28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

It sounds like you're asking about a class in Java!

A class is essentially a blueprint for creating


objects. It defines properties (variables) and behaviors (methods) that the objects created from
it will have.

Here’s a quick breakdown of how a basic class looks in Java:

Java Class Example

// Define a class named 'Car'

public class Car {

// Instance variables (properties)

String make;

String model;

int year;

// Constructor (a special method to initialize objects)

public Car(String make, String model, int year) {

this.make = make;

this.model = model;

this.year = year;

// Method to display car details

public void displayDetails() {

System.out.println("Car Make: " + make);

System.out.println("Car Model: " + model);

System.out.println("Car Year: " + year);

// Main method to run the program


public static void main(String[] args) {

// Creating an object (instance) of the Car class

Car myCar = new Car("Toyota", "Corolla", 2021);

// Calling the method to display car details

myCar.displayDetails();

Explanation:

1. Class Declaration:
public class Car {} – This defines a class named Car.

2. Instance Variables:
Inside the class, we have properties like make, model, and year which are specific to
each instance of the class.

3. Constructor:
The constructor public Car(String make, String model, int year) is used to initialize the
object with values when it’s created.

4. Method:
The method displayDetails() is used to print out information about the car.

5. Main Method:
The main method is where the program starts running. Here we create an instance of
the Car class (myCar) and then call its method displayDetails() to print the car's details.

Running the Program

When you run the program, you’ll see the following output:

Car Make: Toyota

Car Model: Corolla

Car Year: 2021

Let me know if you'd like to dive deeper into specific class concepts or if you need help with
anything else in Java!

You might also like