Using method outside function that created instance of Class

So anyone can tell me how I can use car1.CheckYear(); outside the If function?
I tried making an instance on top of my code like:

Car car1("","",0); This gives me a return of 0, as if he doesn't overwrite with 1990

if I create Car car1; He tells me I need parameters.

class Car {
  public:
    string brand;   
    string model;
    int year;
    Car(string x, string y, int z) {
      brand = x;
      model = y;
      year = z;
   
     void CheckYear() {
       Serial.println(year)
     }
};

void setup() {
  Serial.begin(9600);
}

void loop() {
  if(true) {
    Car car1("Honda", "Civic", 1990);
  }
  car1.CheckYear();
}

You are missing the closing brace of the constructor

 Car(string x, string y, int z) {
      brand = x;
      model = y;
      year = z;
} <== MISSING

and car1 is local to the if statement so goes right away

Do you want to dynamically instanciate the car or statically?

If you want to change an attribute (instance variable) of an existing instance you can either accès it directly since you made it public or have to write a setter method for doing so, like void changeYear(string aNewYear)

The 'car1' variable has to be in the scope where you call the method "CheckYear". If you don't know what values you want to create 'car1' with when you create it, one way is to make it a pointer to be filled in later. This compiles and produces the desired result.

void loop()
{
  static Car honda("Honda", "Civic", 1990);
  Car *car1 = NULL;


  if (true)
  {
    car1 = &honda;
  }
  
  if (car1 != NULL)
    car1->CheckYear();
}

If you want to be able to create the object with default attributes and fill them in later, you need a new constructor and a new method:

class Car
{
  public:
    String brand;
    String model;
    int year;


    Car()
    {
      brand = "";
      model = "";
      year = 0;
    }


    Car(String x, String y, int z)
    {
      brand = x;
      model = y;
      year = z;
    }


    void setParams(String x, String y, int z)
    {
      brand = x;
      model = y;
      year = z;
    }


    void CheckYear()
    {
      Serial.println(year);
    }
};


void setup()
{
  Serial.begin(9600);
}


void loop()
{
  Car car1;


  if (true)
  {
    car1.setParams("Honda", "Civic", 1990);
  }


  car1.CheckYear();
}