• Post Reply Bookmark Topic Watch Topic
  • New Topic

Program problem  RSS feed

 
Saad Zahoor
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • 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 :
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • 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.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!