• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

This may be easy

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And yet, I am lost. I need help making this method work. Everything before the comment was provided by the instructor but the rest of this method is giving me loads of trouble. I am going to try to explain this as best as I can, I am way too new at this
On this line towards the end..

out.printBalance(balance);
I am getting the error "balance cannot be resolved to a variable".

The printBalance method in the Output class was declared as
public void printBalance(double balance)

Here is my code
 
Ranch Hand
Posts: 479
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

So the behavior/method has been declared as:

public void printBalance(double balance)



But when you are invoking the same from inside handleLogin() you are making a call to

out.printBalance(balance);

correct?

Inside this method you have not declared a variable 'balance'.

Cheers,
Raj.
 
Mark Captain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raj Kamal wrote:Hello,

So the behavior/method has been declared as:

public void printBalance(double balance)



But when you are invoking the same from inside handleLogin() you are making a call to

out.printBalance(balance);

correct?

Inside this method you have not declared a variable 'balance'.

Cheers,
Raj.



Thank you that has indeed solved the problem there. Now is that the only place i have to declare that variable? I have other variables elsewhere in the program and i think the problem is that, in general, i do not know where to declare these variables.
 
Rajkamal Pillai
Ranch Hand
Posts: 479
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Think of variables (declaration) this way:-

When the compiler encounters a variable it needs to know a few details like how much memory to allocate for it and what values can be stored in it. Now that is why you have to declare them before they can be used.

Please keep in mind this and hence before you use any variable you have to declare them. So if a variable has been declared in any block {} then it will be visible inside it (including any sub-blocks).

Raj.
 
Ranch Hand
Posts: 171
Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have defined a variable as class member so it can be viewed in any of the method in the class also in sub class which inherits (only when the modifier defined as public/protected) and variables declared in blocks (as Raj said) and inside a function then it is a local variable and can't accessed by any other function or sub class which inherits it. For more clarification you can read the 'variable scope' topic.
 
Marshal
Posts: 79468
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kumaravadivel Subramani wrote:If you have defined a variable as class member so it can be viewed in any of the method in the class also in sub class which inherits (only when the modifier defined as public/protected) . . .

I hope you always use private for all your fields. Any handling of a field is inside the class, or via a method. Methods can of course be public.
 
Campbell Ritchie
Marshal
Posts: 79468
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where did you create the account object in that method? You will be returning null, and letting nulls loose in your code is a dangerous practice. Either create a valid account object with the new operator, or throw an Exception. You can put most of that method inside a loop, so you go back if the account is not created correctly.

Why have you got an Output class? Why not use System.out?
 
Campbell Ritchie
Marshal
Posts: 79468
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark, Its good to get some more idea on local variables, final variables, globals and member variable etc, that will help you to find out these kind of error quickly. I would suggest head first Java for absolute beginner .
 
Ranch Hand
Posts: 83
Netbeans IDE MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Provide full code so that we can understand structre
reply
    Bookmark Topic Watch Topic
  • New Topic