Almac3
Almac3

Reputation: 63

joining multiple variables into one single variable

I have just started using android studio, and was wondering is there anyway possible to combine multiple variables into one variable to later use as a global variable? These are the varaibles I am using (to create a date and time picker):

int dayNow, monthNow, yearNow, hourNow, minuteNow;

I just want to create one variable now say 'timeslected' which combines all 5 of the variables above? If anyone could help would be greatly appreciated.

Upvotes: 1

Views: 1440

Answers (1)

simpleApps
simpleApps

Reputation: 774

In this case you should create a new class which contains all of these variables. :

public class Time{
    private int minute, hour, day, month, year;

    public Time(int minute, int hour, int day, int month, int year) {
        this.minute = minute;
        this.hour = hour;
        this.day = day;
        this.month = month;
        this.year = year;
    }

    public int getMinute() {
        return minute;
    }

    public void setMinute(int minute) {
        this.minute = minute;
    }
}

Create a new Time object:

Time time = new Time(48, 13, 2, 11, 2017);

Then you can access to this variables with the get- / and set-methods:

int minute = time.getMinute();

Upvotes: 1

Related Questions