GeekInterview.com
Series: Subject: Topic:
Question: 19 of 267

What is the difference between Instance and Static variable

Asked by: Interview Candidate | Asked on: Nov 10th, 2011
Showing Answers 1 - 3 of 3 Answers
mujeeb3108

Answered On : Nov 15th, 2011

Static variable: A value assigned to a static variable will be shared across all instances of the class.

Instance variable: A value assigned to an instance variable is local to that object and it will be different for each instance of that class.

  
Login to rate this answer.
mujeeb3108

Answered On : Nov 15th, 2011

Static variable: A value assigned to a static variable will be shared across all instances of the class. Instance variable: Each instance variable will hold different value that will not be share among instances, On the other hand an instance variable may have different values across every different instance of the class.

Code
  1. class MyClass
  2. {
  3. int myInstance;
  4. static int myStatic=10;
  5.  
  6. public:
  7. putvalues(int my)
  8. {
  9. myInstance = my;
  10. }
  11. getvalues()
  12. {
  13. printf("myInstance %d",myInstance);
  14. printf("myStatic %d",myStatic);
  15. };
  16. main()
  17. {
  18. MyClass a,b;
  19. a.putvalues(100);
  20. b.putvalues(200);
  21. a.getvalues();
  22. b.getvalues();
  23.  
  24. }
  25.  
  26. output:
  27. a:
  28. myInstance 100
  29. myStatic      10
  30. b:
  31.  
  32. myInstance 200
  33. myStatic      10
  34.  

Yes  1 User has rated as useful.
  
Login to rate this answer.
pavani

Answered On : Feb 18th, 2012

Where I have to write clrscr() in a CPP programme

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.