Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Trying to learn classes.

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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: 2230
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks to both of you, got it fixed!
 
Marshal
Posts: 69779
277
  • Mark post as helpful
  • send pies
  • 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.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic