This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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
Posts: 23811
140
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
Posts: 23811
140
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.
 
Everybody's invited. Except this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!