C# C# OOP: Static Instance Explained

  • Thread starter Thread starter FrankJ777
  • Start date Start date
  • Tags Tags
    Oop Static
AI Thread Summary
Instantiating a class as static means that the instance is shared across all instances of the class, while a non-static instance is unique to each instance of the class. In the discussion, the static Queue instance allows access without needing to create an instance of the containing class, using Classname.messageBuffer. This creates a singleton-like behavior. The difference becomes apparent when modifying the Queue's data; changes to a static instance affect all references to it, whereas changes to a non-static instance only affect that specific instance. Testing the behavior by modifying the Queue and observing the output from the constructor can help clarify these differences.
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
 
Technology 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top