Prints: main,B.m1 Why isn't this a compiler error at B.m1("main");? Is it because they are in the same file and that tells class C where B is? Although m1(String s) is a static method, I didn't think that B was created until new was used on it. Is the compiler doing something for me since the code is asking for B.m1("main");?
posted 14 years ago
John, Welcome to the JavaRanch! Static methods can be invoked directly on the class: there is no need for an instance of the class. Please note the statement B.m1("main"); does not make use of a reference to an instance of the class B. Instead, the method is invoke on the class--not on an instance of the class.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Thanks, for the response, it started to make more sense to me after I posted it. I started to think about the other static methods that are in your exams like Math.round(0.5f);, I don't have an instance of Math either but I can use the methods.
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads