aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Calling static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Calling static methods" Watch "Calling static methods" New topic
Author

Calling static methods

Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19061
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
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

Joined: Sep 28, 2004
Posts: 19061
    
  40



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
Poonam Kohli
Ranch Hand

Joined: Jun 10, 2009
Posts: 53
replace lines 1 and 2 as below

inst.x();
inst.y();
Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling static methods