How to use a class and have two different values stored in it java
- User
How to use a class and have two different values stored in it java
- Assistant
To use a class and have two different values stored in it in Java, you can create a class with two variables and provide methods to set, get and modify these variables. 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; } public void sayHello() { System.out.println("Hello, my name is " + name + " and I am " + age + " years old."); } }In the above example, we have created a class called Person with two instance variables: name and age. We have also provided setter and getter methods for these variables to set and retrieve their values. Additionally, we have created a sayHello() method that will print out the name and age of the Person.