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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Back
Top