4.8 Basic Java Syntax: Classes and Objects
Java is an object-oriented programming language, which means it uses concepts such as classes and objects to model the real world. Understanding these concepts is essential for anyone who wants to learn Java. In this section, we will explore the basic syntax of classes and objects in Java.
Classes in Java
A class in Java is a kind of template or blueprint from which objects are created. It defines a custom data type, specifying the attributes (variables) and behaviors (methods) that objects of that type will have. A class is like a contract that specifies what objects can do.
The basic syntax for defining a class in Java is as follows:
public class ClassName {
// Attributes (instance variables)
typeData attribute1;
typeData attribute2;
// Constructor
public ClassName(parameterDatatype1, parameterDatatype2) {
this.attribute1 = parameter1;
this.attribute2 = parameter2;
}
// Methods
returntype nameMethod(parameters) {
// Method body
}
}
Attributes are the properties that class objects will have, such as name, age or balance, for example. They are usually declared at the beginning of the class.
The constructor is a special method that has the same name as the class and is used to initialize new objects. It is automatically invoked when a new object is created.
Methods are functions that define the behavior of class objects. They can perform operations on attributes, calculate values, among other actions.
Objects in Java
An object is an instance of a class. When an object is created, it inherits all attributes and behaviors defined by the class. Creating an object in Java is done using the new
keyword, followed by calling the class constructor.
The syntax for creating an object is:
ClassNameObjectName = new ClassName(valueToAttribute1, valueToAttribute2);
After creating an object, it is possible to access its attributes and invoke its methods using the dot operator (.
).
ObjectName.MethodName(arguments);
variableDatatype = ObjectName.attribute1;
Practical example
Let's consider a simple class called Car
. This class will have attributes such as make, model and year, as well as a method to display information about the car.
public class Car {
String tag;
String template;
int year;
public Car(String brand, String model, int year) {
this.brand = brand;
this.model = model;
this.year = year;
}
public void displayInformation() {
System.out.println("Brand: " + brand + ", Model: " + model + ", Year: " + year);
}
}
To create an object of the Car
class and use its methods, we would do the following:
public class TestCar {
public static void main(String[] args) {
Car myCar = new Car("Toyota", "Corolla", 2020);
myCar.displayInformation();
}
}
In this example, the main
method creates a new myCar
object of the Car
class with specific values for make, model and year. Then, it calls the exibirInformacoes
method to print the car details.
Encapsulation and Access Modifiers
It is good programming practice to encapsulate the attributes of a class, which means making them private and accessing them through public methods known as getters and setters. This provides control over how values are accessed and modified and helps maintain data integrity.
The private
, public
and protected
access modifiers control the visibility of a class's members (attributes and methods) to other classes.
A private attribute cannot be accessed directly from outside its class. Public methods are needed to allow this interaction in a controlled manner.
Conclusion
Understanding the basic syntax of classes and objects is fundamental to programming in Java. Classes provide the framework for creating complex, interactive objects, while objects are the concrete instances that operate within the program. By encapsulating data and providing access methods, you can ensure more secure and maintainable programming.
Now that you understand the basic syntax of classes and objects in Java, you are ready to begin exploring more advanced concepts such as inheritance, polymorphism, and interfaces, which are equally important in becoming a competent Java programmer.