This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes simple class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple class" Watch "simple class" New topic
Author

simple class

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


After I run code, It returns 0!

why?
Reehan Lalkhanwar
Ranch Hand

Joined: Jun 16, 2007
Posts: 106
Am guessing that sek is actually sekeh and others too...
m was never initialized and so you get zero.


Thank you,
Reehan
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

I don't know how you got 0 as this class won't even compile, errors are on lines 8, 9, 10, 13 and 17.

Can you see why you are getting errors?

Sean


I love this place!
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

And check out line 13. Are you sure that is the m want to assign the value to?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


What about now?
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

As Wouter says above, you may want to make sure you are doing exactly what you want to do on line 13.

Also there is still a compilation error on line 17.

Sean
Javad Rashidi
Ranch Hand

Joined: Feb 13, 2011
Posts: 34

try this


http://www.myviwo.com
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Javad Rashidi wrote:try this . . .
And tell us why it doesn't work; you have made a common beginner's error

Tell us what would happen if you change 415000 to 4150000, and why.
Javad Rashidi
Ranch Hand

Joined: Feb 13, 2011
Posts: 34

what about this


415000: result=1224300000
4150000: result=-1203167296
honestly i don't know why?
could you tell me why?
Reehan Lalkhanwar
Ranch Hand

Joined: Jun 16, 2007
Posts: 106
I think its due to overflow.
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Check the value of Integer.MAX_VALUE and compare it to the value that you would expect to get with 4150000.

Sean
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
The reason you got 0 the first time was that you had the keyword int before the name of the variable (I think it was m). That means you are creating a new local variable m which “shadows” the field of the same name. You change the local m and leave the field unchanged.
Whoever said "overflow" was correct. If you google, you find this, for example. The pictures are helpful.
zameel amjed
Greenhorn

Joined: Feb 16, 2011
Posts: 14

Javad Rashidi wrote:try this
Result will print 0 again due to Overflow
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

No, not because of overflow. I'll quote Wouter because he's already identified the issue:
Wouter Oet wrote:And check out line 13. Are you sure that is the m want to assign the value to?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
No, zameel amjed, I am afraid you are mistaken on that point.
Tarun wadhwa
Greenhorn

Joined: Jan 29, 2011
Posts: 14

Hi Abalfazl Hossein,

the code you have mentioned in line 17 there is a mistake

mehe a= new meh(500,14000,1000) ;

change mehe to meh.

and the reason why it returns the value of "m" 0(Zero) when you print it because you have made it static and static variables are loaded when the first time class name has been called and it can't see Instance Variable or non static methods.

And by default value of int is assigned 0(Zero), that's why you are getting Zero when you try to print that

I hope now the things will be more clearer and understandable to you.


"The More you Play With it, The more you will learn ". "Keep Playing"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Tarun wadhwa wrote: . . . and the reason why it returns the value of "m" 0(Zero) when you print it because you have made it static and static variables are loaded when the first time class name has been called and it can't see Instance Variable or non static methods. . . .
Nonsense. That is quite mistaken.

The reason for getting 0 printed was that m was shadowed by the mistaken declaration of a local variable.
Wouter Oet, Sean Clark and I have already told you that, some of us more clearly than others.
Tarun wadhwa
Greenhorn

Joined: Jan 29, 2011
Posts: 14

hi campbell ritchie,

i apologize for my big mistake.

thanks for highlighting my mistakes.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Apology accepted
The important bit: can you see what the mistake was, and remember it for next time? Mistakes are not serious, as long as they are found and corrected. That is why you are here: to get rid of mistakes and misconceptions.
 
Consider Paul's rocket mass heater.
 
subject: simple class