Back to: Java Tutorials
Introduction
In Java, an object is an instance of a class. A class is a blueprint or template that defines the variables and methods common to all objects of that type. Once you have defined a class, you can create objects from it and interact with those objects using the methods and variables defined in the class. This tutorial will cover the basics of creating and using Java class objects with code examples and charts.
Defining a Java Class
To define a class in Java, you use the “class” keyword followed by the name of the class. Here’s an example:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
In this example, we define a class called “Person” with two private variables, “name” and “age”, and getter and setter methods for those variables.
Creating a Java Class Object
To create an object from a Java class, you use the “new” keyword followed by the name of the class and any arguments required by the class’s constructor. Here’s an example:
Person person1 = new Person("John", 30);
In this example, we create a new object of type “Person” called “person1” and pass in the values “John” and 30 for the “name” and “age” variables, respectively.
Accessing Java Class Object Variables and Methods
Once you have created a Java class object, you can access its variables and methods using dot notation. Here are some examples:
String person1Name = person1.getName();
int person1Age = person1.getAge();
person1.setName("Jane");
In these examples, we use the “getName” and “getAge” methods to retrieve the values of the “name” and “age” variables for the “person1” object. We also use the “setName” method to change the value of the “name” variable for the “person1” object to “Jane”.
Java Class Object Output
To see the output of our Java class object, we can use the “System.out.println” method to print the values of its variables. Here’s an example:
System.out.println(person1.getName() + " is " + person1.getAge() + " years old.");
In this example, we print the value of the “name” variable for the “person1” object followed by the value of the “age” variable.
Java Class Object Chart
Here is a chart summarizing some of the key aspects of Java class objects:
Characteristic | Description |
---|---|
Definition | A blueprint or template for creating objects |
Variables | Data members that hold values and state for each object |
Methods | Functions that define the behavior and actions of each object |
Constructor | A special method that creates instances of the class |
Access modifiers | Keywords that specify the visibility and accessibility of class members |
Dot notation | The syntax used to access an object’s variables and methods |
Output | The result of using an object’s methods and variables |
Example | An instance of a class created using the “new” keyword and the class’s constructor |
Complete code example
Here is an example code with output for a Java object:
public class Car {
private String make;
private String model;
private int year;
public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public void printCarDetails() {
System.out.println("Make: " + make);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}
}
public class CarApp {
public static void main(String[] args) {
Car myCar = new Car("Toyota", "Camry", 2020);
System.out.println("Car Details:");
myCar.printCarDetails();
myCar.setYear(2021);
System.out.println("Updated Year: " + myCar.getYear());
}
}
Output:
Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.
Follow us on social media
Follow Author