I have a class with two methods defined in it.
public class Routines { public static method1() { /* set of statements */ } public static method2() { /* another set of statements.*/ } }
Now I need to call method1() from method2()
Which one the following approaches is better? Or is this qualify as a question?
public static method2() { method1(); }
OR
public static method2() { Routines.method1(); }
While I agree with the existing answers that this is primarily a style issue, it is enough of a style issue that both Eclipse and IntelliJ’s code critics will flag ‘non-static references to static methods’ in code that does not use the
Classname.method()style.I made it a habit to emphasize intent by using the classname to qualify references to static targets,
thisto qualify references to instance targets, and bare names for local references. A modern IDE will use different highlighting for these constructs, so I suppose it is less important these days. I like for the maintainer (often myself) to know what was intended, that yes, I knew that was astaticreference.Yeah, it does make for slightly more verbose code, but I think it is worth the extra characters.