File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling a method w/out an object

 
Ben Hultin
Ranch Hand
Posts: 135
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look up what static methods mean.
 
Ben Hultin
Ranch Hand
Posts: 135
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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:



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic