static-methods

PHP: What if I call a static method in non-static way

北城余情 提交于 2019-11-27 14:59:57
I'm not pro in Object Oriented Programming and I got a silly question: class test { public static function doSomething($arg) { $foo = 'I ate your ' . $arg; return $foo; } } So the correct way to call doSomething() method is to do test::doSomething('Pizza'); , Am I right? Now, what will happen if I call it like this: $test = new test; $bar = $test->doSomething('Sandwich'); I've tested it and it's working without any error or notice or etc. but is that correct to do this? user1708452 As Baba already pointed out, it results in an E_STRICT depending on your configuration. But even if that's no

Calling static method on a class?

孤人 提交于 2019-11-27 14:44:13
Say, I have a reference to a Class object with SomeType having a static method. Is there a way to call that method w/o instantiating SomeType first? Preferably not escaping strong typing. EDIT: OK, I've screwed up. interface Int{ void someMethod(); } class ImplOne implements Int{ public void someMethod() { // do something } } Class<? extends Int> getInt(){ return ImplOne.class; } In this case someMethod() can't be static anyways. A static method, by definition, is called on a class and not on an instance of that class. So if you use: SomeClass.someStaticMethod() you are instantiating nothing

calling another method from the main method in java [duplicate]

你离开我真会死。 提交于 2019-11-27 14:41:57
问题 This question already has an answer here: calling non-static method in static method in Java [duplicate] 14 answers I have class foo{ public static void main(String[] args){ do(); } public void do(){} } but then when I call do() from main by running the command java foo on the command line, java complains that you can't call a method from a static function. So my question is: How do you call methods from the main method and if it is not possible what are some alternative strategies to call

How to use Dependency Injection with Static Methods?

天涯浪子 提交于 2019-11-27 14:08:57
问题 Imagine there is a Customer class with an instance Load() method. When the Load() method is called, it retrieves order details by e.g. var orders = Order.GetAll(customerId, ...); GetAll() is a static method of the Order class and the input parameters are fields defined in the Customer class. As you can see, Order is a dependency of the Customer class, however, I can't just create an IOrder and inject it there as interfaces can't have static methods. Therefore, the question is how could I

In Java, is there any disadvantage to static methods on a class?

假装没事ソ 提交于 2019-11-27 13:52:53
Lets assume that a rule (or rule of thumb, anyway), has been imposed in my coding environment that any method on a class that doesn't use, modify, or otherwise need any instance variables to do its work, be made static. Is there any inherent compile time, runtime, or any other disadvantage to doing this? (edited for further clarifications) I know the question was somewhat open ended and vague so I apologize for that. My intent in asking was in the context of mostly "helper" methods. Utility classes (with private CTORs so they can't be instantiated) as holders for static methods we already do.

What is the difference between static methods in a Non static class and static methods in a static class?

戏子无情 提交于 2019-11-27 12:02:41
I have two classes Class A and ClassB: static class ClassA { static string SomeMethod() { return "I am a Static Method"; } } class ClassB { static string SomeMethod() { return "I am a Static Method"; } } I want to know what is the difference between ClassA.SomeMethod(); and ClassB.SomeMethod(); When they both can be accessed without creating an instance of the class, why do we need to create a static class instead of just using a non static class and declaring the methods as static? The only difference is that static methods in a nonstatic class cannot be extension methods . In other words,

How can I dynamically create class methods for a class in python [duplicate]

时光毁灭记忆、已成空白 提交于 2019-11-27 10:51:10
This question already has an answer here: Adding a Method to an Existing Object Instance 16 answers If I define a little python program as class a(): def _func(self): return "asdf" # Not sure what to resplace __init__ with so that a.func will return asdf def __init__(self, *args, **kwargs): setattr(self, 'func', classmethod(self._func)) if __name__ == "__main__": a.func I receive the traceback error Traceback (most recent call last): File "setattr_static.py", line 9, in <module> a.func AttributeError: class a has no attribute 'func' What I am trying to figure out is, how can I dynamically set

Static method behavior in multi-threaded environment in java

自古美人都是妖i 提交于 2019-11-27 10:01:36
There's a simple stupid question that bother me and make several arguments in my mind. I want to throw out all the doubts about below questions. class Clstest{ public static String testStaticMethod(String inFileStr) { // section 0 // section 1 // do something with inFileStr // section 2 // section 3 return inFileStr; } } Let's assume there are five threads are each executing a call to Clstest.testStaticMethod("arg-n") at the same time. Thread 1 calls Clstest.testStaticMethod("arg-1") . When thread 1 is in the section 1, thread 2 calls Clstest.testStaticMethod("arg-2") . Then what will happen

Are static methods more efficient?

强颜欢笑 提交于 2019-11-27 09:58:02
问题 In terms of memory and time, is it better to make a method static? 回答1: Usually yes, there is no need to pass the "this" reference. That reference is passed in the ECX register so there is no additional stack space required. The register is already set if you make the call from an instance method of the same class, there will be no savings at all. But it can help relieving the pressure on a x86 CPU core when the method is in another class, x86 doesn't have a lot of registers. Seeing a

Why is a static method considered a method?

旧街凉风 提交于 2019-11-27 09:33:29
问题 I'm writing an explanation for some code for a course, and have been accidentally using the words method and function interchangeably. I decided to go back over and fix the wording, but ran into a hole in my understanding. From what I understand, a subroutine is a function if it doesn't act on an instance of a class (its effect is restricted to its explicit input/output), and is a method if it operates on an instance of a class (it may carry out side effects on the instance that make it