This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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
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.
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.
author & internet detective
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.