aspose file tools*
The moose likes Beginning Java and the fly likes Problem with executing simple code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with executing simple code" Watch "Problem with executing simple code" New topic
Author

Problem with executing simple code

Laurent Jones
Greenhorn

Joined: Jun 27, 2011
Posts: 6
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

Joined: May 26, 2003
Posts: 30915
    
158

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Laurent Jones
Greenhorn

Joined: Jun 27, 2011
Posts: 6
Ohh I see!! Thank's a lot for your help!
Benjamin Thvedt
Ranch Hand

Joined: Jul 29, 2011
Posts: 31
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

Joined: May 26, 2003
Posts: 30915
    
158

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

Joined: Jul 29, 2011
Posts: 31
Whoops! My bad.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with executing simple code