wood burning stoves*
The moose likes Beginning Java and the fly likes calling a method w/out an object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calling a method w/out an object" Watch "calling a method w/out an object" New topic
Author

calling a method w/out an object

Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
I am looking to call a method but w/out an object to assign it to. Is this possible? I just want to call a method from a driven file and have it execute in the driver. All the resources I look up discuss method calling with objects and assigning the values to that particular object.


Thanks a lot for your help
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Look up what static methods mean.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
Thanks a lot for helping me out, I read up on static methods and managed to get most of the errors taken care of except for these two:



GreedGame.java:22: incompatible types
found : java.lang.String
required: Greed
methodCall = Greed.setUp();
^
GreedGame.java:29: incompatible types
found : int
required: Greed
methodCall = Greed.rollDice();
^
2 errors




Im guessing the compiler thought that Greed was a type when actually its the driven class. These methods were called from the driver class as so:



My static methods are:




My variables have been declared as such:




my object has been initialized as:





Thanks a lot for your help in the matter
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

setup() returns a String so you will need a String variable for that. Likewise, rollDice() returns an int so you will need an int variable.
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
Thanks a lot, I tried the suggestion but I still seem to get the same error as before.


here is what I have added to the setUp() and rollDice() methods:


fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

"methodCall" is declared as a "Greed" type of object:
Greed methodCall = new Greed();

The problem is with this line:

but the 'setup' method returns a String:
public static String setUp() {


your problem line says "get a String object (from the method call) and stuff it into a Greed object." you can't do that. you're trying to put a square peg into a round hole. Since the method returns a string, you need to either:

a) Change the method to return a 'Greed' type object
b) Change the variable where you are storing the return value to be a String.

I'd go for b:




There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calling a method w/out an object
 
Similar Threads
Thread Ques
Need help on deadly diamond of death..
Q about the Object need your help?
Get more error in the below program