Questions tagged [static-classes]

In object oriented programming a static class is a class whose members must be accessed without an instance of the class. Its members must be created as static.

In object oriented programming a static class is a class whose members must be accessed without an instance of the class. Its members must be created as static.

289 questions
22
votes
4 answers

Extension Methods vs Instance Methods vs Static Class

I'm a little bit confused about the different ways to use methods to interact with objects in C#, particularly the major design differences and consequences between the following: Invoking an instance method Using a static class on a POCO Creating…
johnnyRose
  • 7,310
  • 17
  • 40
  • 61
20
votes
3 answers

How can I create a static class in Swift?

I'm looking to create a static class called VectorCalculator. Perhaps this function should just be placed in my Vector class (similar to NSString's --stringByAppendingString method in Obj-C)... and if you think that... let me know. Anyway I want to…
chris P
  • 6,359
  • 11
  • 40
  • 84
18
votes
4 answers

Should I never use static methods and classes and singletons when following the Test Driven Development paradigm

I've been reading that static methods, static classes, and singletons are evil when you try to implement unit testing in your project. When following the TDD paradigm, should I just forget that they ever existed and never use them again or is it ok…
Mr Grok
  • 3,876
  • 5
  • 31
  • 40
17
votes
1 answer

Eclipse has some problems with auto import static classes

I have some problems with eclipse. if I use something like anyMap() in my source code, and press then CTRL + SHIFT + O no update in the import list will be done. If I write something like: import static org.mockito.Matchers.* into my imports, the…
Joergi
  • 1,527
  • 3
  • 39
  • 82
15
votes
2 answers

Refactoring a static class to use with dependency injection

We need to use an unmanaged library in our code that has static methods. I'd like to introduce the library operation as a dependency in my code. And apart from having static methods, the library has an initialization method and a settings method,…
hattenn
  • 4,371
  • 9
  • 41
  • 80
15
votes
3 answers

Laravel : Calling functions defined in base_controller from view

In using the laravel framework, how can I call a function defined in base_controller, in a view. For exacmple: class Base_Controller extends Controller { public static function format_something() { return something; } } How…
Jim
  • 517
  • 2
  • 5
  • 11
14
votes
1 answer

How to inject into static classes using Dagger?

I want to introduce dependency injection through Dagger to a project. The following code acts as an example to describe the problem of injection into static classes. The static method setupTextView() is called from multiple classes: public abstract…
JJD
  • 50,076
  • 60
  • 203
  • 339
13
votes
3 answers

Static class declaring a protected member

I'm reading the book "C# Language", and hit this note from Vladimir Reshetnikov: If a static class declares a protected or protected internal member, a compile-time error occurs (CS1057). May I know why? What's wrong with a static class having…
athos
  • 6,120
  • 5
  • 51
  • 95
13
votes
4 answers

Abstract classes vs Static classes in C#

Possible Duplicate: What's the difference between an abstract class and a static one? Hello I Would like to know what are all the differences between abstract classes and static classes in C# When do I use what and why? Is it true the abstract…
Dan
  • 141
  • 1
  • 1
  • 4
13
votes
1 answer

Strange behavior on static members of a class - How's this possible?

Consider the following class: public class MyClass { public static string[] SomeAmazingConsts = { Const1 }; public static string Const1 = "Constant 1"; public static string Const2 = "Constant 2"; } Now, check out the usage: class…
Joe Bank
  • 653
  • 7
  • 20
13
votes
3 answers

Extension methods on a static class?

I know i can do the below to extend a class. I have a static class i would like to extend. How might i do it? I would like to write ClassName.MyFunc() static public class SomeName { static public int HelperFunction(this SomeClass v)
user34537
13
votes
2 answers

ASP.NET Static classes and asp.net sessions

Would someone be kind enough to explain or point to article that explains how the scope of static classes and methods interact with the scope of a ASP.NET user session. Here is the more specific situation which explains my question: User A logs…
BlueChameleon
  • 924
  • 1
  • 10
  • 36
13
votes
4 answers

Low-level difference: non-static class with static method vs. static class with static method

I was wondering what are the general benefits (or drawbacks) of using a non-static class with a static method versus a static class with the same static method, other than the fact that I cannot use static methods from a non-static class as…
Den
  • 16,686
  • 4
  • 47
  • 87
12
votes
3 answers

Static inner class in python

My code needs to have an inner class and I want to create the instance of this inner class without creating the instance of outer class. How to do so in python? In java we can define the inner class to be static but I don't know how to make a inner…
Abhishek Gupta
  • 6,465
  • 10
  • 50
  • 82
12
votes
3 answers

Using an inner class name and an object name same in Java

In the following code snippet, presumably it appears that it should issue some compilation error but it doesn't: class Outer { public static class Inner { static String obj = "Inner"; } static Optional Inner = new Optional(); …
Tiny
  • 27,221
  • 105
  • 339
  • 599
1
2
3
19 20