Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

have to prefix “Math.” to them?

Math prefix
0
10 Posted

have to prefix “Math.” to them?

0
10

[*] No. There is no good alternative. There are several bad alternatives: 1. Using “import” doesn’t work. The import stament only imports packages, subpackages, and classes, not class members. This doesn’t work: import java.lang.Math.*; 2. Minimizing class name usage is unclear and bad style. – You could wrap the functions in your own class. double sin(double x) { return Math.sin(x); } // etc. for each function But you’d have to use your class name everywhere but inside your class, so it doesn’t help. – You can make a null reference to the Math class and use it to refer to the static methods. Declare java.lang.Math M = null; angle = M.cos(i); Besides not being clear, this invites abuse and errors. – You could inherit the names If java.lang.Math were not final and your class did not extend another class, you could have your class extend Math, to bring the namespace in. However, it is poor OOP style to use inheritance to obtain a name abbreviation rather than to express a type hierarchy.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123