• 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

Calling static methods

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI All,

I have written a sample program which I cannot compile, please help:



Line 1 and 2 throw compile time errors:
For Line 1 I get: "Multiple markers at this line
- The operator + is undefined for the argument type(s) String, void
- The static method x() from the type B should be accessed in a static way"

For Line 2, i get:
"The operator + is undefined for the argument type(s) String, void" I thought that even though method doesn't return anything
you can still call a method from the system out if it prints to screen?? Seems like a very basic issue I am missing, please demystify for me!


Please let me know: How I can call the static methods and run this program? Thanks.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I thought that even though method doesn't return anything
you can still call a method from the system out if it prints to screen?? Seems like a very basic issue I am missing, please demystify for me!



It has nothing to do with the method being static, the issue is that the compiler can't do the string concat...



The x() method doesn't return anything -- it is a void type. So, how do you do a string concat in this expression?

Henry
 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I have now replaced lines 1 and 2 by:



But still getting compile errors:
Line 1 gives me: Multiple markers at this line
- The static method x() from the type B should be accessed in a static way
- The method println(boolean) in the type PrintStream is not applicable for the arguments
(void)

Line 2 gives me: The method println(boolean) in the type PrintStream is not applicable for the arguments (void)

Any ideas , much appreciated.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Take a look at the JavaDoc for the java.lang.System class... actually, the java.io.PrintStream class of which is the type of the "out" variable. You will notice that the println() method is overloaded to take a whole bunch of types.

Your x() method returns a void type. From the JavaDocs, is there a println() method that takes a void type as a parameter?

Henry
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
replace lines 1 and 2 as below

inst.x();
inst.y();
 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys, I got it... just removed it from the system.out.println() and I was able to clear my doubt
about static and instance method overridding.
 
reply
    Bookmark Topic Watch Topic
  • New Topic