From the course: Oracle Java Foundations

Unlock this course with a free trial

Join today to access over 24,700 courses taught by industry experts.

Static: Demo

Static: Demo

(thoughtful music) - [Joe] Exercise 6-3: Use static constants. We're going to modify the MIN_PRICE and TAX_RATE constants in the Clothing class to be associated with the class rather than an instance of Clothing by making it static. And then in the main method, before we've instantiated any Clothing objects, we'll print a message that contains a minimum price value. So we return to the Clothing class, and you'll notice, of course, that we have our minimum price and minimum tax, which are final, making them constants. But we also want them accessible through the class, so we're going to change this from private to public. So now we can access it, and accessing constants are fine. And we're also going to make it static, indicating that it belongs to the class rather than the instance. So now, to access this variable, all we have to do is reference the class name itself. We can do Clothing.MIN_PRICE or Clothing.MIN_TAX and access those values. Since they're constants, they can't change…

Contents