Java synchronized static methods: lock on object or class

Just to add a little detail to Oscar’s (pleasingly succinct!) answer, the relevant section on the Java Language Specification is 8.4.3.6, ‘synchronized Methods’: A synchronized method acquires a monitor (ยง17.1) before it executes. For a class (static) method, the monitor associated with the Class object for the method’s class is used. For an instance method, … Read more

Unresolved external symbol on static class members

If you are using C++ 17 you can just use the inline specifier (see https://stackoverflow.com/a/11711082/55721) If using older versions of the C++ standard, you must add the definitions to match your declarations of X and Y unsigned char test::X; unsigned char test::Y; somewhere. You might want to also initialize a static member unsigned char test::X … Read more

Synthetic Class in Java

Java has the ability to create classes at runtime. These classes are known as Synthetic Classes or Dynamic Proxies. See http://java.sun.com/j2se/1.5.0/docs/guide/reflection/proxy.html for more information. Other open-source libraries, such as CGLIB and ASM also allow you to generate synthetic classes, and are more powerful than the libraries provided with the JRE. Synthetic classes are used by … Read more

C# static class constructor

C# has a static constructor for this purpose. static class YourClass { static YourClass() { // perform initialization here } } From MSDN: A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is … Read more

Why does calling a method in my derived class call the base class method?

There’s a difference between new and virtual/override. You can imagine, that a class, when instantiated, is nothing more than a table of pointers, pointing to the actual implementation of its methods. The following image should visualize this pretty well: Now there are different ways, a method can be defined. Each behaves different when it is … Read more

How to implement private method in ES6 class with Traceur [duplicate]

There are no private, public or protected keywords in current ECMAScript 6 specification. So Traceur does not support private and public. 6to5 (currently it’s called “Babel”) realizes this proposal for experimental purpose (see also this discussion). But it’s just proposal, after all. So for now you can just simulate private properties through WeakMap (see here). … Read more

How to document class attributes in Python?

To avoid confusion: the term property has a specific meaning in python. What you’re talking about is what we call class attributes. Since they are always acted upon through their class, I find that it makes sense to document them within the class’ doc string. Something like this: class Albatross(object): “””A bird with a flight … Read more

putting current class as return type annotation [duplicate]

In python-3.7 this issue has been resolved by not evaluating the annotations at function definition time. Instead, they are preserved in __annotations__ in string form. This is called Postponed Evaluation of Annotations, introduced in PEP 563. Also note: Deprecation policy Starting with Python 3.7, a __future__ import is required to use the described functionality. No … Read more