• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with executing simple code

 
Laurent Jones
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I'm just going through the head first java book. The weird thing is it seems like i understand what i've learnt in java so far theoretically, but when I go to write the code myself, It's all over the place.

I wrote this up:


Now i know I haven't got a main method, but where do I put the main method and what do I put in the main method to print out ("Total is " + z.)

I've tried putting the main method underneath public class TestStuff but didn't work, I put it underneath all of the code but didn't work, I tried calling the go method by writing new go(); and called the takeTwo(); but no luck.

Please help so I can get the basics right from the start
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34229
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are really close. If you changed your go method signature to public static void main(String[] args), you'd be good to go. The go() method is creating an object and calling the method which is exactly what you want your driver method to do.
 
Laurent Jones
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh I see!! Thank's a lot for your help!
 
Benjamin Thvedt
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new to java myself, personally I would have tried to put the main in some different class, but I didn't know classes could have an object of themselves, within themselves, like that, if that makes sense. You know I would have the teststuff class with the method, and then I would hava a different class "TheProgram.java" or whatever that would have a teststuff object in it that I could use, if that makes sense.

Something like this:


I'm pretty new myself, but I think that would work. Then I would run TheProgram and see if that works. Just another way is all.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34229
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benjamin,
That's close to working. You'd need to make the variable static or better yet, put it inside the main method. The reason is that it is currently an instance variable and a static method cannot access an instance variable.
 
Benjamin Thvedt
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoops! My bad.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic