C# OOP: Static Instance Explained

  • Context: C# 
  • Thread starter Thread starter FrankJ777
  • Start date Start date
  • Tags Tags
    Oop Static
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 5K views
FrankJ777
Messages
140
Reaction score
6
I have a question about instatiating a class as static and what that implies. Going through an old project of mine I noticed I did the following:

public static Queue messageBuffer = new Queue();

how does that static instance of the Queue behave compared to one declared without the static modifier? For example:

public Queue messageBuffer = new Queue();

Does it make a difference. I've played with both instances but can't tell the difference, but i want to make sure I know what I"m doing.

Thanks
 
Physics news on Phys.org
FrankJ777 said:
I have a question about instatiating a class as static and what that implies. Going through an old project of mine I noticed I did the following:

public static Queue messageBuffer = new Queue();

how does that static instance of the Queue behave compared to one declared without the static modifier? For example:

public Queue messageBuffer = new Queue();

Does it make a difference. I've played with both instances but can't tell the difference, but i want to make sure I know what I"m doing.

Thanks

Have you modified messageBuffer (both static and other) data (attributes) and checked what happens to data when you create second instance that contains this object?

And, I would also suggest that some kind of output whenever Queue constructor is called (like Console.WriteLine("Queue is created") inside the Queue constructor.).
 
The static keywords turns the field into a singleton, this also makes it possible to access it without creating the containing class by using Classname.messageBuffer.