| Author |
Dan's Comprehensive Exam #8
|
John Hembree
hired gun
Ranch Hand
Joined: Mar 07, 2003
Posts: 250
|
|
Question 10 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");?
|
 |
Dan Chisholm
Ranch Hand
Joined: Jul 02, 2002
Posts: 1865
|
|
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>
|
 |
John Hembree
hired gun
Ranch Hand
Joined: Mar 07, 2003
Posts: 250
|
|
|
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.
|
 |
 |
|
|
subject: Dan's Comprehensive Exam #8
|
|
|