aspose file tools*
The moose likes Beginning Java and the fly likes variable name in constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "variable name in constructor" Watch "variable name in constructor" New topic
Author

variable name in constructor

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


This code is wrong, I know it is because amount has the same name, But I don't know what happened actually!

It shows:
name;Amir Account profit for one year=0.0

Amir shows despite same variable, But amount is 0!!
May some one explain more?
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

You are declaring amount again in your constructor at line 6. This declaration hides your field, so that amount represents variable local to the constructor, and the value is never assigned to the field it should be assigned to.

Edit: Actually, that code shouldn't even compile. Constructor name does not match class name.


The quieter you are, the more you are able to hear.
Sergii Trotsenko
Greenhorn

Joined: Oct 23, 2012
Posts: 6
delete type declaration (double) on line 6.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Sergii Trotsenko welcome to the Ranch
Sergii Trotsenko
Greenhorn

Joined: Oct 23, 2012
Posts: 6
Thanks. If my answer was not in "Ranch style" please let me know
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Sergii Trotsenko wrote:delete type declaration (double) on line 6.


It was nice point my friend. You are welcome!!Thanks all folks!!
 
wood burning stoves
 
subject: variable name in constructor