Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple class

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


After I run code, It returns 0!

why?
 
Reehan Lalkhanwar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am guessing that sek is actually sekeh and others too...
m was never initialized and so you get zero.
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And check out line 13. Are you sure that is the m want to assign the value to?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What about now?
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what about this


415000: result=1224300000
4150000: result=-1203167296
honestly i don't know why?
could you tell me why?
 
Reehan Lalkhanwar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think its due to overflow.
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49776
69
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
Notepad PHP Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javad Rashidi wrote:try this
Result will print 0 again due to Overflow
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, zameel amjed, I am afraid you are mistaken on that point.
 
Tarun wadhwa
Greenhorn
Posts: 14
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi campbell ritchie,

i apologize for my big mistake.

thanks for highlighting my mistakes.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic