• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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: 23830
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: 23830
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.
 
Too many men are afraid of being fools - Henry Ford. Foolish tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!