• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Program problem

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


Class







Resultt

Your Name = null
Your Age = 0
Your Roll Number = 0
Your Name = Saad Zahoor
Your Age = 18
Your Roll Number = 31192
Your Name = Talha Ejaz
Your Age = 19
Your Roll Number = 39982
Your Name = Usman Mirza
Your Age = 18
Your Roll Number = 43312





Question 1 : Why it is giving First as Null or 0 .
Question 2 : Why the result is not in Order :
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Saad,

You have a problem with using static when you declare that variables name, age, and rollnumber.  Since you use static in your declaration, then each of your instances of Machine not only use the same variable name, but they are using the same actual variable--saad, talha, usman, ahmed instances of Machine all address the very same name, the very same age, and the very same rollnumber in each of their instances. Once you set one of those variables in any instance, then each instance sees the same change because it is the same variable shared by all.

Les
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very basic question: moving.

Did you make that method static, and then get a compiler error going on about non‑static? I think that error message is confusing; it gives people the impression that you need to add the keyword static to whatever you are trying to access. The correct response however is to remove the keyword static from your current location, or move the code out into a different location. Had you made the method in line 36 non‑static, you might not have had this problem.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Question 1 : Why it is giving First as Null or 0 .


Even after you remove the "static" notation for your variables, and make getInformation() non-static, you will still have this problem. Look at your constructior - it calls getInformation(). And what does that do? It writes out some information. And what is that information going to look like?
 
They worship nothing. They say it's because nothing lasts forever. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic