static-methods

Variable sharing inside static method

瘦欲@ 提交于 2019-12-10 02:06:49
问题 I have a question about the variables inside the static method. Do the variables inside the static method share the same memory location or would they have separate memory? Here is an example. public class XYZ { Public Static int A(int value) { int b = value; return b; } } If 3 different user calls execute the method A XYZ.A(10); XYZ.A(20); XYZ.A(30); at the same time. What would be the return values of each call? XYZ.A(10)=? XYZ.A(20)=? XYZ.A(30)=? 回答1: They're still local variables - they

Why is overriding of static methods left out of most OOP languages?

▼魔方 西西 提交于 2019-12-10 00:30:56
问题 It is certainly not for good OOP design - as the need for common behavior of all instances of a derived class is quite valid conceptually. Moreover, it would make for so much cleaner code if one could just say Data.parse(file) , have the common parse() code in the base class and let overriding do its magic than having to implement mostly similar code in all data subtypes and be careful to call DataSybtype.parse(file) - ugly ugly ugly So there must be a reason - like Performance ? As a bonus -

Access to static properties via this.constructor in typescript

℡╲_俬逩灬. 提交于 2019-12-09 14:10:23
问题 I want to write es6 class: class SomeClass { static prop = 123 method() { } } How to get access to static prop from method() without use SomeClass explicitly? In es6 it can be done with this.constructor , but in typescript this.constructor.prop causes error " TS2339: Property 'prop' does not exist on type 'Function' ". 回答1: but in typescript this.constructor.prop causes error "TS2339: Property 'prop' does not exist on type 'Function'". Typescript does not infer the type of constructor to be

What is the difference between all-static-methods and applying a singleton pattern?

∥☆過路亽.° 提交于 2019-12-09 11:26:59
问题 I am making a database to store information about the users of my website (I am using stuts2 and hence Java EE technology). For the database I'll be making a DBManager. Should I apply singleton pattern here or rather make all it's methods static? I will be using this DBManager for basic things like adding, deleting and updating User profiles. Along with it, I'll use for all other querying purposes, for instance to find out whether a username already exists and to get all users for

Static method get - is this bad practice?

蓝咒 提交于 2019-12-09 08:20:25
问题 Had a discussion with a colleague about wether this is bad practice or not. Now I can not find immediate examples of this online. We have a lot of database object mappers and call it's functions like so (example) - the setId method get's the row from the database and set's it to predefined propertys class Person { public static function get($id) { $object = new Person; $object->setId($id); return $object; } } Using it like this we can use simple constructions like this: (where we got the id

How to refer current class using decltype in C++11?

蹲街弑〆低调 提交于 2019-12-08 21:38:41
问题 When I declare a class static method, is it possible to refer current class using decltype (or in any other similar style)? For example, class AAA { static AAA const make(); }; I am trying to make something like this. class AAA { static decltype(*this) const make(); // Not working because there's no `this`. }; The *this is used to describe what I want to do. I want to know some decltype() expression which can be resolved to AAA . If it's possible how can I do that? 回答1: In C++1y you could do

Java Static Field Initialization

感情迁移 提交于 2019-12-08 18:36:58
问题 I have just spent half an hour figuring this thing out, I have managed to fix my code, but I don't fully understand what is going on and wondered if someone could shed some light on it. I have a utils type class, that contains a few static fields (a database connection endpoint for example) that are used by various other programs depending on the task at hand. Essentially a library. This is how it looked previously (while still broken); //DBUtils.java public final class DBUtils { private

Write to static field - is FindBugs wrong in this case?

不打扰是莪最后的温柔 提交于 2019-12-08 17:26:40
问题 I have a Java class like this: public class Foo { public static int counter = 0; public void bar(int counter) { Foo.counter = counter; } } FindBugs warns me about writing to the static field counter via the instance method bar . However, if I change the code to: public class Foo { public static int counter = 0; public static void setCounter(int counter) { Foo.counter = counter; } public void bar(int counter) { setCounter(counter); } } Then FindBugs won't complain. Isn't that wrong? I'm still

implement a virtual method from the base class as derived in static

☆樱花仙子☆ 提交于 2019-12-08 07:58:28
问题 I have an abstract base class with a virtual method. In the derived class, this method is implemented. However, I want the function in the derived class as a static method in order to be able to call the function without instantiating an object of that class. class Base { virtual double Foo(double rParam) const; }; class Derived1 : public Base { static double Foo(double rParam); }; class Derived2 : public Base { static double Foo(double rParam); }; Essentially, Derived1 and Derived2 provide

How To Convert Templated Function Overloads to Partial-Specialized Templated Class Static Methods?

三世轮回 提交于 2019-12-08 05:40:02
问题 I have several functions that I want to specialize based on type qualities, such as "character, signed-integer, unsigned-integer, floating-point, pointer"; using type_traits seems like the way to do this, and have code similar to the to the following: #include <tr1/type_traits> #include <iostream> template<bool, typename _Tp = void> struct enable_if { }; template<typename _Tp> struct enable_if<true, _Tp> { typedef _Tp type; }; template< typename T > inline void foo_impl( typename enable_if<