• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

calling a method w/out an object  RSS feed

 
Ranch Hand
Posts: 135
  • 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
 
Sheriff
Posts: 21459
96
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look up what static methods mean.
 
Ben Hultin
Ranch Hand
Posts: 135
  • 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
Posts: 21459
96
Chrome Eclipse IDE Java Windows
  • 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
  • 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:


 
lowercase baba
Bartender
Posts: 12627
50
Chrome Java Linux
  • 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
Boost this thread!