static-methods

What happens when a static method is invoked using a null object reference?

被刻印的时光 ゝ 提交于 2020-01-09 07:34:05
问题 public class CallingStaticMethod { public static void method() { System.out.println("I am in method"); } public static void main(String[] args) { CallingStaticMethod csm = null; csm.method(); } } Can someone explain how the static method is invoked in the above code? 回答1: It's been optimized away by the compiler, simply because having an instance of the class is not necessary. The compiler basically replaces csm.method(); by CallingStaticMethod.method(); It's in general also a good practice

non-static method “getActivity” cannot be referenced from static context [duplicate]

好久不见. 提交于 2020-01-05 17:36:52
问题 This question already has answers here : non-static method getActivity() (4 answers) Closed 3 years ago . i make a method static but i have this problem: android.support.v4.app.FragmentTransaction transaction2 = getActivity() .getSupportFragmentManager().beginTransaction();.... getActivity() is underline in red by IDE why? I show you my code THANKS IN ADVANCE EVERYBODY! FRAGMENT: public class MyListFragment extends Fragment implements android.widget.CompoundButton.OnCheckedChangeListener {

PHP static function being called in dynamic environment

こ雲淡風輕ζ 提交于 2020-01-05 16:33:33
问题 Since when PHP allows to call static function like a dynamic function? I am using php 5.3.2 class weird{ public static function iamstatic($calledFrom){ echo "I am a static function called with a $calledFrom operator\n"; } public function test(){ self::iamstatic("static"); $this->iamstatic("dynamic"); } } $c = new weird(); $c->test(); weird::iamstatic("Static outside class"); $c->iamstatic("Dynamic outside class"); This outputs : I am a static function called with a static operator I am a

Undefined reference to static method

纵然是瞬间 提交于 2020-01-05 02:41:29
问题 This is probably something simple but I can't figure it out. In my main.cpp I have: Image image("/path/to/image.png"); int* h = ImageProcessor::dailyPrices(image); And in my ImageProcessor.h I have: //Maybe something is wrong with this? I am trying to forward declare. namespace Magick { class Image; } class ImageProcessor { public: ImageProcessor(); virtual ~ImageProcessor(); static int* dailyPrices(const Magick::Image& abp_chart); }; And in ImageProcessor.cpp I have int* dailyPrices(const

Undefined reference to static method

无人久伴 提交于 2020-01-05 02:41:24
问题 This is probably something simple but I can't figure it out. In my main.cpp I have: Image image("/path/to/image.png"); int* h = ImageProcessor::dailyPrices(image); And in my ImageProcessor.h I have: //Maybe something is wrong with this? I am trying to forward declare. namespace Magick { class Image; } class ImageProcessor { public: ImageProcessor(); virtual ~ImageProcessor(); static int* dailyPrices(const Magick::Image& abp_chart); }; And in ImageProcessor.cpp I have int* dailyPrices(const

How can I provide a scala companion object's class to Java?

橙三吉。 提交于 2020-01-04 16:57:29
问题 I have a Java code that looks for annotations in static methods of a class. processor.readStatics( MyClass.class ); // Takes Class<?> How can I provide the methods of a scala companion object to this function from within scala? class MyClass { } object MyClass { def hello() { println("Hello (object)") } } I seems that: MyClass$.MODULE$.getClass() should be the answer. However, MyClass$ seems to be missing from scala (in 2.10, at least) and only visible to Java. println( Class.forName("MyClass

Ruby: Alter class static method in a code block

一世执手 提交于 2020-01-04 05:17:07
问题 Given the Thread class with it current method. Now inside a test, I want to do this: def test_alter_current_thread Thread.current = a_stubbed_method # do something that involve the work of Thread.current Thread.current = default_thread_current end Basically, I want to alter the method of a class inside a test method and recover it after that. I know it sound complex for another language, like Java & C# (in Java, only powerful mock framework can do it). But it's ruby and I hope such nasty

static method with polymorphism in c++

五迷三道 提交于 2020-01-04 02:53:12
问题 I have a weird issue using polymorphism. I have a base class that implements a static method. This method must be static for various reasons. The base class also has a pure virtual method run() that gets implemented by all the extended classes. I need to be able to call run() from the static class. The problem, of course, is that the static class doesn't have a this pointer. This method can be passed in a void * parameter. I have been trying to come up with a clever way to pass the run method

How laravel use $this context in static methods?

烂漫一生 提交于 2020-01-03 13:37:11
问题 How does Laravel uses $this->comment() method inside console.php file in "routes" directory while Artisan::command() being a static method? <?php use Illuminate\Foundation\Inspiring; Artisan::command('inspire', function() { $this->comment(Inspiring::(quote)); })->describe('Display an inspiring quote'); 回答1: $this isn't being used inside the static method itself, it's used in the closure that's passed to that method. From the Laravel manual: The Closure is bound to the underlying command

Unit-testing a class that calls a static method

[亡魂溺海] 提交于 2020-01-03 13:04:50
问题 I am trying to unit-test a class 'A' which calls a static method of a class 'B'. Class 'B' essentially has a google guava cache which retrieves a value(Object) from the cache given a key, or loads the object into the cache (in case of a cache-miss) using a service adapter. The service-adapter class in turn has other autowired dependencies to retrieve the object. These are the classes for illustration purposes: Class A public class A { public Object getCachedObject(String key) { return B