Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: `static` Keyword | OOP Essentials
C# Beyond Basics

bookChallenge: `static` Keyword

In this problem, you need to:

  • Create a new private field called totalCars of type double which tracks the total number of Car objects created;
  • Create a getter method called getTotalCars which simply returns the number of objects;
  • Make sure the value of the totalCars field is incremented every time a new Car object is created.
index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738
using System; class Car { int modelYear; double mileage; string brandName; public Car(string brandName, int modelYear, double mileage) { this.brandName = brandName; this.modelYear = modelYear; this.mileage = mileage; // Write code below this line // Write code above this line } // Write code below this line // Write code above this line } class ConsoleApp { static void Main() { Console.WriteLine(Car.getTotalCars()); Car car1 = new Car("Toyota", 2022, 25.5); Car car2 = new Car("Honda", 2020, 30.2); Car car3 = new Car("Ford", 2021, 28.8); Console.WriteLine(Car.getTotalCars()); } }
  1. The field which tracks the number of objects should be static because it always stores some data.
  2. It was explained in the Access Modifiers chapter that a getter method is simply a public method which returns the value of a private field. In this case getTotalCars should return totalCars.
  3. The getTotalCars method should also be static because we want to use it without an instance.
index.cs

index.cs

copy
123456789101112131415161718192021222324252627282930313233343536373839404142
using System; class Car { int modelYear; double mileage; string brandName; public Car(string brandName, int modelYear, double mileage) { this.brandName = brandName; this.modelYear = modelYear; this.mileage = mileage; // Write code below this line totalCars += 1; // Write code above this line } // Write code below this line private static double totalCars; public static double getTotalCars() { return totalCars; } // Write code above this line } class ConsoleApp { static void Main() { Console.WriteLine(Car.getTotalCars()); Car car1 = new Car("Toyota", 2022, 25.5); Car car2 = new Car("Honda", 2020, 30.2); Car car3 = new Car("Ford", 2021, 28.8); Console.WriteLine(Car.getTotalCars()); } }
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 6

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you show me how to declare a static private field in Java?

How do I increment the static field in the constructor?

Can you explain why the getter method should be static?

Awesome!

Completion rate improved to 2.04

bookChallenge: `static` Keyword

Swipe to show menu

In this problem, you need to:

  • Create a new private field called totalCars of type double which tracks the total number of Car objects created;
  • Create a getter method called getTotalCars which simply returns the number of objects;
  • Make sure the value of the totalCars field is incremented every time a new Car object is created.
index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738
using System; class Car { int modelYear; double mileage; string brandName; public Car(string brandName, int modelYear, double mileage) { this.brandName = brandName; this.modelYear = modelYear; this.mileage = mileage; // Write code below this line // Write code above this line } // Write code below this line // Write code above this line } class ConsoleApp { static void Main() { Console.WriteLine(Car.getTotalCars()); Car car1 = new Car("Toyota", 2022, 25.5); Car car2 = new Car("Honda", 2020, 30.2); Car car3 = new Car("Ford", 2021, 28.8); Console.WriteLine(Car.getTotalCars()); } }
  1. The field which tracks the number of objects should be static because it always stores some data.
  2. It was explained in the Access Modifiers chapter that a getter method is simply a public method which returns the value of a private field. In this case getTotalCars should return totalCars.
  3. The getTotalCars method should also be static because we want to use it without an instance.
index.cs

index.cs

copy
123456789101112131415161718192021222324252627282930313233343536373839404142
using System; class Car { int modelYear; double mileage; string brandName; public Car(string brandName, int modelYear, double mileage) { this.brandName = brandName; this.modelYear = modelYear; this.mileage = mileage; // Write code below this line totalCars += 1; // Write code above this line } // Write code below this line private static double totalCars; public static double getTotalCars() { return totalCars; } // Write code above this line } class ConsoleApp { static void Main() { Console.WriteLine(Car.getTotalCars()); Car car1 = new Car("Toyota", 2022, 25.5); Car car2 = new Car("Honda", 2020, 30.2); Car car3 = new Car("Ford", 2021, 28.8); Console.WriteLine(Car.getTotalCars()); } }
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 6
some-alt