Understanding Static Members

Static members 

belong to the class itself rather than individual objects.

Instance members

They can be used only after an instance of a class is created.

Static members

Is used to declared members that do not belong to individual objects but to a class itself.

one common example is the main method that serves as the entry point.


Static members belong to the class itself, not the instance that created from the class. 

When an instance of a class created

A separate copy is created for each instance field.

But only one copy of static field is shared by all instance.

All things that you create are non-static by default.

What can you make static

  • Classes
  • Method
  • properties

What static means:

Does not belong to any object instance created, it belongs to the class itself.

A class is a template and object is created from that template so it's instantiated from that template.
that is the class.

When we make something static that belongs to the class itself, it belongs to the template.
 
So whenever you're instantiating the class and you get your object, you cannot use that static member from the object instance you have created.


Static method

When you create a static method you can only access it through the class itself rather than the instance of that class.



  • When a class is static all our members have to be static.
  • you cannot inherit a static. 
  • A static class cannot be instantiated.


You are developing a C# application. You need to decide whether to declare a class
member as static. Which of the following statements is true about static members of a class?

a) You can use the this keyword reference with a static method or property.
b) Only one copy of a static field is shared by all instances of a class.
c) Static members of a class can be used only after an instance of a class is created.
d) The static keyword is used to declare members that do not belong to individual objects but to a class itself.

Answer: d
Difficulty: Medium
Section Reference: Understanding Static Members
The static keyword is used to declare members that do not belong to individual objects but to a class itself. A static member cannot be referenced through an instance object. Instead, a static member is referenced through the class name. It is not possible to use the this keyword reference with a static method or property because the this keyword can be used only to access instance objects.

Suppose that you are a new C# developer and are reviewing object-oriented programming fundamentals. Which of the following statements is not true?

a) A class is a concrete instance of an object.
b) A class defines the template for an object.
c) A class is a definition of a new data type.
d) A constructor is used to initialize the data members of the object.

Answer: a
Difficulty: Medium
Section Reference: Understanding Static Members
A class is not a concrete instance of an object. Instead, an object is a concrete instance of a class. The facts in the other answer choices are all correct.

Comments

Popular posts from this blog

Understanding Encapsulation

Understanding Polymorphism