• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dan's Comprehensive Exam #8

 
hired gun
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");?
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
John Hembree
hired gun
Posts: 250
MS IE Oracle Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic