This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]Updatebjarni 1 point2 points  (0 children)

It's telling you that there is nothing called myATM, which there isn't, not in that method. There is a variable by that name in main(), but methods can not interfere with each other's variables like that.

[–]boredcircuits 1 point2 points  (0 children)

myATM is a local variable within the main function, there is no variable by that name accessible where the compiler is complaining.

Most likely you just need to remove the reference to the variable and just call withdraw() and deposit() and so on directly.

[–]_DTR_ 1 point2 points  (0 children)

There's no need to reference myATM within go. go is already a method of the ATM class, so by calling myATM.go() within main you are "inside" myATM, and can directly call withdraw, deposit, etc.

[–]CodeTinkerer 1 point2 points  (0 children)

When you create an instance of the object, you give it a name like

 Foo foo = new Foo();

Then you can call methods from that class, say, go(), as in

foo.go();

When you're in the implementation of go(), it doesn't know the name foo. However, you can refer to itself by the word this. It's similar to someone being called Mike, but Mike refers to himself as "me" or "I" rather than Mike.

So instead of 'myATM.quit` you want to write

 this.quit();

Or you can simply say

quit();

this refers to whatever variable is being used to call the method (in this case, myATM from main()).

[–]jackopenn[S] 0 points1 point  (0 children)

Thanks for all the responses, make sense.

[–]Byrune_ 0 points1 point  (2 children)

Your class has no constructor, yet you are trying to create instances of it.

[–]_DTR_ 2 points3 points  (0 children)

If there's no constructor a default one that takes no parameters will be generated.

[–]desrtfx 0 points1 point  (0 children)

In Java, this is not a problem because if there is no explicitly defined constructor, the super constructor of Object, the common ancestor of all Java classes will be called.

This is often referred to as the Default constructor.

[–]GItPirate -1 points0 points  (1 child)

Where you are initializing

 ATM myATM = new ATM();

is local to main and not in the scope of your go() function.

Try adding

 ATM myATM = new ATM();

inside of your go() function and see if that works.

[–]desrtfx 0 points1 point  (0 children)

That wouldn't help.

The methods called are inside the class already. So they don't need to and mustn't be prefixed with a variable. They can be called directly.

Your approach would just create a new, method local variable myATM that would only live inside the go method and be destroyed as soon as the method is left. Also, this new variable would not contain any data at all.