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
2059
votes
28 answers

Java inner class and static nested class

What is the main difference between an inner class and a static nested class in Java? Does design / implementation play a role in choosing one of these?
Omnipotent
  • 27,619
  • 12
  • 30
  • 34
1141
votes
14 answers

Static Classes In Java

Is there anything like static class in Java? What is the meaning of such a class? Do all the methods of the static class need to be static too? Is it required the other way round as well? That if a class contains only static methods, the class shall…
Kraken
  • 23,393
  • 37
  • 102
  • 162
314
votes
8 answers

This Handler class should be static or leaks might occur: IncomingHandler

I'm developing an Android 2.3.3 application with a service. I have this inside that service to communicate with Main activity: public class UDPListenerService extends Service { private static final String TAG = "UDPListenerService"; …
VansFannel
  • 45,055
  • 107
  • 359
  • 626
109
votes
3 answers

Why static classes cant implement interfaces?

Possible Duplicate: Why Doesn’t C# Allow Static Methods to Implement an Interface? In my application I want to use a Repository that will do the raw data access (TestRepository, SqlRepository, FlatFileRepository etc). Because such a repository…
Boris Callens
  • 90,659
  • 85
  • 207
  • 305
61
votes
13 answers

C# Static class vs struct for predefined strings

A co-worker just created the following construction in C# (the example code is simplified). His goal was to shorten the notation for all predefined strings in the rest of the code. public struct PredefinedStrings { public const string…
Rob van Groenewoud
  • 1,824
  • 1
  • 13
  • 17
60
votes
6 answers

ASP.NET Core Web API Logging from a Static Class

I'm logging just fine using dependency injection on my controllers, now I need to log something from a static class. How can I log from a static class? I can't use dependency injection because it's static and I can't just pass in an existing logger…
47
votes
11 answers

What is a "static" class?

In C# what is the difference between: public static class ClassName {} And: public class ClassName {}
Jeremy H
  • 1,784
  • 4
  • 17
  • 27
46
votes
3 answers

Exception in static constructor

I've dug around SO for an answer to this, and the best one I can find so far is here, however that is geared toward instances with static constructors; I'm only using the class statically. My code: public static class MailHelper { private…
James King
  • 6,233
  • 5
  • 42
  • 63
44
votes
4 answers

How to pass parameter to static class constructor?

I have a static class with a static constructor. I need to pass a parameter somehow to this static class but I'm not sure how the best way is. What would you recommend? public static class MyClass { static MyClass() { …
MrProgram
  • 5,044
  • 13
  • 58
  • 98
40
votes
5 answers

Why does Android prefer static classes

I see a lot of java code where android prefers to have developers use static inner classes. Particularly for patterns like the ViewHolder Pattern in custom ListAdapters. I'm not sure what the differences are between static and non-static classes.…
Jeremy Edwards
  • 14,620
  • 17
  • 74
  • 99
37
votes
5 answers

@Autowired in static classes

This is an Spring MVC project with Hibernate. I'm, trying to make a Logger class that, is responsible for inputting logs into database. Other classes just call proper methods with some attributes and this class should do all magic. By nature it…
T.G
  • 1,913
  • 1
  • 16
  • 29
35
votes
9 answers

Why is a static class illegal in Java?

I'm developing an Android application but have hit a bit of a brick wall, I keep getting the error: Illegal modifier for the class FavsPopupFragment; only public, abstract & final are permitted This happened after following this answer to another…
JDx
  • 2,615
  • 3
  • 22
  • 33
26
votes
8 answers

Why do members of a static class need to be declared as static? Why isn't it just implicit?

Obviously there can't be an instance member on a static class, since that class could never be instantiated. Why do we need to declare members as static?
richard
  • 12,263
  • 23
  • 95
  • 151
25
votes
7 answers

Should you avoid static classes?

Are static classes considered bad practice? I read an article about this a couple days ago (can't find it, sorry) which basically said that having static classes (especially those 'helper' classes) are typically a sign of bad code. Is this correct,…
Alex
  • 75,813
  • 86
  • 255
  • 348
24
votes
4 answers

How can I run a static initializer method in C# before the Main() method?

Given a static class with an initializer method: public static class Foo { // Class members... internal static init() { // Do some initialization... } } How can I ensure the initializer is run before Main()? The best I can…
Matt
  • 21,026
  • 18
  • 63
  • 115
1
2 3
19 20