Questions tagged [static-access]

in object-oriented programming, access from a static (class) context, as opposed to one from a particular object instance context

In object-oriented programming, access from a static (class) context, as opposed to one from a particular object instance context.

See also: 'static-classes' tag wiki at Stack Overflow:

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.

37 questions
43
votes
7 answers

Why the static data members have to be defined outside the class separately in C++ (unlike Java)?

class A { static int foo () {} // ok static int x; // <--- needed to be defined separately in .cpp file }; I don't see a need of having A::x defined separately in a .cpp file (or same file for templates). Why can't be A::x declared and defined…
iammilind
  • 2,232
  • 4
  • 24
  • 35
35
votes
3 answers

Why is there no static keyword in Kotlin?

Kotlin is known primarily as a drop-in replacement for Java, but it gets rid of a well-known Java construct: the static keyword. Instead, that class-level functionality is offered mainly by companion objects. What is wrong with static methods and…
user1446
  • 472
  • 5
  • 7
28
votes
3 answers

Are static classes with static methods considered SOLID?

SOLID includes the Liskov substitution princicple which has the notion that “objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program”. Since static classes with static methods (a…
Pacerier
  • 4,973
  • 7
  • 39
  • 58
18
votes
4 answers

Java - Is it a bad idea to have fully static classes?

I'm working on a larger solo project and right now, and I have several classes in which I do not see any reason to create an instance of. My dice class right now, for example, stores all of its data statically and all of its methods are static too.…
13
votes
1 answer

Why is there no facility to overload static properties in PHP?

Intro PHP allows you to overload method calls and property accesses by declaring magic methods in classes. This enables code such as: class Foo { public function __get($name) { return 42; } } $foo = new Foo; echo $foo->missingProperty; //…
Jon
  • 527
  • 3
  • 15
12
votes
2 answers

Static is bad, but what about the Factory pattern?

I'm on a TDD project, so I try to stick as much as possible to the good pratices involved with that kind of development. One of them is avoiding as much as possible static and global. I'm facing this problem: I've an object "article" that can have…
FMaz008
  • 413
  • 3
  • 9
12
votes
2 answers

Best practice for creating a 'global' config class used by numerous components

I have a large project with a driver part and about 5 libraries doing various associated tasks. Many of the libraries require access to 'global' configuration data which is read from a database at startup by the driver code. By driver I just mean…
user619818
  • 1,757
  • 2
  • 14
  • 23
10
votes
2 answers

Drawback of implementing DDD service as static class?

Can Services in a domain-driven design be implemented as C# static class? What are the drawbacks of this choice? Can it be implemented as a non-singleton non-static class?
Louis Rhys
  • 6,042
  • 11
  • 42
  • 59
6
votes
2 answers

Deterministic statics vs inject-able classes

I have in mind several deterministic functions that I'd like to put together, but I'm struggling with the full impllications of doing: Static class with static methods Instance of a class that can be injected into consumers In both cases, the…
Kritner
  • 394
  • 1
  • 10
6
votes
2 answers

Issues about static injection in Spring?

I use spring-boot with spring xml in my project. I wrapper the DAOs in a DataAccessService class to serve as a DB service layer, both the service and the DAOs are injected in spring xml and used by Autowired. Now I want to have a XXXutils class to…
shangyin
  • 196
  • 1
  • 1
  • 6
5
votes
1 answer

Scoping recommendations while developing in C

While developing a library using C, what are your recommendations about variable and function scoping? In C++, OOP and namespaces made the whole thing a lot easier. But how to do that with plain C? Specially how to use the keywords static and extern…
Gulshan
  • 9,402
  • 10
  • 58
  • 89
5
votes
1 answer

Why shouldn't a static class have an internal state?

While working on a project, I decided to create a database class to manage my DB connection. I started looking for the best practice to do that, which is usually either a static class or a singleton pattern class. Answers I've found, such…
5
votes
3 answers

Static class vs Singleton class in C#

Possible Duplicate: What is the difference between all-static-methods and applying a singleton pattern? I need to make a decision for a project I'm working of whether to use static or singleton. After reading an article like this I am inclined to…
radu florescu
  • 171
  • 1
  • 1
  • 6
4
votes
3 answers

Why use sealed instead of static on a class?

Our system has several utility classes. Some people on our team use (A) a class with all-static methods and a private constructor. Others use (B) a class with all-static methods (these the juniors). On code analysis, (A) and (B) raise warning…
sq33G
  • 280
  • 3
  • 12
3
votes
2 answers

What is the rationale for making certain methods for data types static?

In C#, for instance, there are static methods for telling if a string is null or empty, finding an element in an array, clearing an array, etc. However there's an instance method for replacing characters in a string, and both static and instance…
Ace
  • 597
  • 4
  • 10
1
2 3