This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Calling static methods  RSS feed

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23586
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 23586
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!