static-methods

Why use classmethod instead of staticmethod? [duplicate]

心已入冬 提交于 2019-12-03 10:45:19
问题 This question already has answers here : Difference between staticmethod and classmethod (25 answers) Closed 6 years ago . I know what they do and I've seen many examples of both, but I haven't found a single example where I would have to use classmethod instead of replacing it with a staticmethod . The most common example of classmethod I've seen is for creating a new instance of the class itself, like this (very simplified example, there's no use of the method atm. but you get the idea):

What is the purpose of a static method in interface from Java 8?

只谈情不闲聊 提交于 2019-12-03 10:43:07
Why are static methods supported from Java 8? What is the difference between the two lines in main method in below code? package sample; public class A { public static void doSomething() { System.out.println("Make A do something!"); } } public interface I { public static void doSomething() { System.out.println("Make I do something!"); } } public class B { public static void main(String[] args) { A.doSomething(); //difference between this I.doSomething(); //and this } } As we can see above, I is not even implemented in B. What purpose would it serve to have a static method in an interface when

Static method get - is this bad practice?

眉间皱痕 提交于 2019-12-03 10:41:13
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 from for-example a post) $person = Person::get($id); instead of $person = new Person; $person->setId($id

module with classes with only static methods

核能气质少年 提交于 2019-12-03 10:12:06
I have a Python module that contains a number of classes, each representing a particular physical material with its properties (e.g., density, specific heat). Some of the properties are just float members of the class, but many depend on some parameter, e.g., the temperature. I implemented this through @staticmethod s, i.e., all of the classes look like class Copper(object): magnetic_permeability = 1.0 @staticmethod def density(T): return 1.0 / (-3.033e-9 + 68.85e-12*T - 6.72e-15*T**2 + 8.56e-18*T**3) @staticmethod def electric_conductivity(T, p): return 1.0141 * T**2 * p @staticmethod def

Are static methods good for scalability?

余生长醉 提交于 2019-12-03 10:03:29
问题 Does static methods and class are good for scalability ? I think so static class/method improves scalability of application and instance methods doesn't scales much. So is it good programming practice to write static method where ever it is possible ? 回答1: It depends on WHY the method is static. If it's static because it truly does not need context, then it will probably scale very well compared to something of similar complexity that is not static because it requires context. However, if it

Handle a paste event in c#

杀马特。学长 韩版系。学妹 提交于 2019-12-03 08:30:45
I've created a static class numeric Textbox but i wan't to control what the users paste in te textbox. For handling paste Event i use textchanged event: static public void textChanged(EventArgs e, TextBox textbox, double tailleMini, double tailleMaxi, string carNonAutorisé) { //Recherche dans la TextBox, la première occurrence de l'expression régulière. Match match = Regex.Match(textbox.Text, carNonAutorisé); /*Si il y a une Mauvaise occurence: * - On efface le contenu collé * - On prévient l'utilisateur */ if (match.Success) { textbox.Text = ""; MessageBox.Show("Votre copie un ou des

Multiple Threads calling static helper method

拈花ヽ惹草 提交于 2019-12-03 07:33:25
I have a web application running on Tomcat. There are several calculations that need to be done on multiple places in the web application. Can I make those calculations static helper functions? If the server has enough processor cores, can multiple calls to that static function (resulting from multiple requests to different servlets) run parallel? Or does one request have to wait until the other request finished the call? public class Helper { public static void doSomething(int arg1, int arg2) { // do something with the args return val; } } if the calls run parallel: I have another helper

Static methods in java interface

二次信任 提交于 2019-12-03 07:09:31
As far as I know you cannot declare static methods in interface body. However, accidentally I found peculiar piece of code on http://docs.oracle.com/ site. Here is the link Namelly public interface TimeClient { void setTime(int hour, int minute, int second); void setDate(int day, int month, int year); void setDateAndTime(int day, int month, int year, int hour, int minute, int second); LocalDateTime getLocalDateTime(); static ZoneId getZoneId (String zoneString) { try { return ZoneId.of(zoneString); } catch (DateTimeException e) { System.err.println("Invalid time zone: " + zoneString + "; using

calling static method in java [duplicate]

旧时模样 提交于 2019-12-03 06:52:25
This question already has answers here : How come invoking a (static) method on a null reference doesn't throw NullPointerException? (5 answers) Possible Duplicate: How come invoking a (static) method on a null reference doesn’t throw NullPointerException? Can any one explain why the output of the following program is " Called " public class Test4{ public static void method(){ System.out.println("Called"); } public static void main(String[] args){ Test4 t4 = null; t4.method(); } } I know we can call static method with class reference , but here I am calling using null reference . please

Python static method is not always callable

廉价感情. 提交于 2019-12-03 06:28:43
While parsing attributes using __dict__ , my @staticmethod is not callable . Python 2.7.5 (default, Aug 29 2016, 10:12:21) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from __future__ import (absolute_import, division, print_function) >>> class C(object): ... @staticmethod ... def foo(): ... for name, val in C.__dict__.items(): ... if name[:2] != '__': ... print(name, callable(val), type(val)) ... >>> C.foo() foo False <type 'staticmethod'> How is this possible? How to check if a static method is callable? I provide