• Post Reply Bookmark Topic Watch Topic
  • New Topic

This may be easy  RSS feed

 
Mark Captain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Kumaravadivel Subramani
Ranch Hand
Posts: 171
Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • 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: 56530
172
  • Mark post as helpful
  • send pies
  • 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: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Javin Paul
Ranch Hand
Posts: 297
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • 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 .
 
vinayak jog
Ranch Hand
Posts: 83
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Provide full code so that we can understand structre
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!