In this tutorial, we aim to introduce the concept of classes and objects in Java, how to create them, and how to use them in your code. By the end of the tutorial, you'll understand these fundamental concepts and be able to create your own classes and objects.
What You Will Learn:
- What are classes and objects in Java
- How to define a class
- How to create an object
- How to use classes and objects
Prerequisites:
- Basic understanding of Java syntax
- Familiarity with the concept of variables and methods
In Java, a class is a blueprint for creating objects. A class can contain fields (variables) and methods to describe the behavior of an object.
An object is an instance of a class. It can have states and behaviors. For example, a car can be an object of the class "Car" with attributes like brand, color, and behaviors like driving, parking.
In Java, a class is created using the keyword class
. Here is a simple template:
class ClassName {
// fields
// methods
}
After creating a class, you can use it to create objects. Here is how to create an object of a class:
ClassName objectName = new ClassName();
Let's define a class named "Car" and create an object from it.
Example 1:
class Car {
String brand; // field
String color; // field
void drive() { // method
System.out.println("Driving...");
}
void park() { // method
System.out.println("Parking...");
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // create an object of class Car
myCar.brand = "Toyota"; // accessing field
myCar.color = "Red"; // accessing field
myCar.drive(); // calling method
myCar.park(); // calling method
}
}
In this example, brand
and color
are fields, and drive()
and park()
are methods. The Main
class creates an object (myCar
) of the Car
class and accesses its fields and methods.
Expected Output:
Driving...
Parking...
In this tutorial, we have learned about classes and objects in Java. We've seen how to define a class with fields and methods, and how to create and use objects from a class. The next steps would be learning about constructors, encapsulation, and inheritance in Java.
Additional Resources:
- Oracle Java Documentation
- W3Schools Java Tutorial
Exercise 1:
Create a class named "Person" with fields "name" and "age", and a method "greet" that prints "Hello, my name is NAME and I'm AGE years old."
Exercise 2:
Create a class named "Calculator" with a method "add" that takes two integers as parameters and returns their sum.
Solutions:
Exercise 1:
class Person {
String name;
int age;
void greet() {
System.out.println("Hello, my name is " + name + " and I'm " + age + " years old.");
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.name = "John";
person.age = 25;
person.greet();
}
}
Exercise 2:
class Calculator {
int add(int a, int b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
System.out.println(calculator.add(5, 7));
}
}
Keep practicing and exploring more about classes and objects. You can also try creating more complex classes with more fields and methods.