Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

simple class

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


After I run code, It returns 0!

why?
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am guessing that sek is actually sekeh and others too...
m was never initialized and so you get zero.
 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What about now?
 
Sean Clark
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 34
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this
 
Marshal
Posts: 75644
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think its due to overflow.
 
Sean Clark
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 75644
354
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 14
PHP Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Sheriff
Posts: 22645
124
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 75644
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, zameel amjed, I am afraid you are mistaken on that point.
 
Greenhorn
Posts: 14
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 75644
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi campbell ritchie,

i apologize for my big mistake.

thanks for highlighting my mistakes.
 
Campbell Ritchie
Marshal
Posts: 75644
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic