Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantages of non-static class over static class?

Tags:

c#

.net

What are the advantages of non-static class over static class?

static class need not be instanciated. so we can directly use ClassName.MemberName, so then whats the use of nonstatic class

like image 360
Kishore Kumar Avatar asked May 23 '12 09:05

Kishore Kumar


People also ask

What is the difference between non-static and static classes?

Difference between static and non-static classStatic class is defined using static keyword. Non-Static class is not defined by using static keyword. In static class, you are not allowed to create objects. In non-static class, you are allowed to create objects using new keyword.

What is the difference between static class and non-static class in Java?

A static variable acts as a global variable and is shared among all the objects of the class. A non-static variables are specific to instance object in which they are created. Static variables occupies less space and memory allocation happens once. A non-static variable may occupy more space.

What is the difference between static and non-static classes in languages that support them )?

A static class means you reuse the same elements without needing to deal with instantiating the same things over and over. Non-static classes allow for data to be different but the operations to be the same.

What is the difference between static class and class?

A static class is similar to a class that is both abstract and sealed. The difference between a static class and a non-static class is that a static class cannot be instantiated or inherited and that all of the members of the class are static in nature.


1 Answers

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself

A static class has only one instance of the class itself so you cannot create multiple instance of a static class.

looking at your question you cannot set two different values to the below property if the class is static because there will be only one instance of ClassName in memory

ClassName.MemberName

more info at

http://msdn.microsoft.com/en-us/library/79b3xss3.aspx

like image 189
Massimiliano Peluso Avatar answered Oct 20 '22 21:10

Massimiliano Peluso