• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Trying to learn classes.

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here I am again. Trying to create this first class (Bank Account), then use it in class (Tester). It tells me it can't recognize variable "balance" when I try to compile class Tester.





I don't know what I'm doing wrong, but it says it can't recognize the variable "balance" in the second set of code.
 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is because there is no variable balance in your Tester class.
If you want to print the balance of your account use myAccount.balance.
 
Greenhorn
Posts: 22
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Justice Smith wrote:Here I am again. Trying to create this first class (Bank Account), then use it in class (Tester). It tells me it can't recognize variable "balance" when I try to compile class Tester.





I don't know what I'm doing wrong, but it says it can't recognize the variable "balance" in the second set of code.



Hi use myAccount.balance instead of balance inside system.out.println

however its not the way to access variables.... read about getters and setters...so whenever outside the class you are trying to access or chnage the value of any variable you should access it through getters and chnage it by using setters
google it you can find so many tutorials on it...

and you are getting that error because you are trying to access non static variable inside static method(here main())
 
Justice Smith
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to both of you, got it fixed!
 
Marshal
Posts: 80937
520
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you fix it?
SW is right to say to use getter methods. I would probably not use set methods in a bank class myself, only deposit and withdraw methods. The balance field should be private.
There is a temptation to fix “non‑static” compiler errors by marking things static. That might sort out the compiler error, but usually makes the design much worse.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic