This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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 Java 8 in Action this week in the Java 8 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: 606


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: 36573
    
  16
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: 606
Sergii Trotsenko wrote:delete type declaration (double) on line 6.


It was nice point my friend. You are welcome!!Thanks all folks!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: variable name in constructor
 
Similar Threads
Ready to give up!
Very Confused!!
Bank account simulation
SubClass Blues!
Help on testprogram and subclass please