static-methods

How to call getClass() from a static method in Java?

倖福魔咒の 提交于 2019-11-26 18:11:12
I have a class that must have some static methods. Inside these static methods I need to call the method getClass() to make the following call: public static void startMusic() { URL songPath = getClass().getClassLoader().getResource("background.midi"); } However Eclipse tells me: Cannot make a static reference to the non-static method getClass() from the type Object What is the appropriate way to fix this compile time error? Just use TheClassName.class instead of getClass() . Sergey Ushakov As for the code example in the question, the standard solution is to reference the class explicitly by

C++ type of enclosing class in static member function

て烟熏妆下的殇ゞ 提交于 2019-11-26 17:48:54
问题 I assume this is outright impossible, but what if. Is it possible to somehow get type of enclosing class in a static member function, in any version of C++? class Impossible { public: static void Fun() { typedef Impossible EnclosingClass; // now do something with EnclosingClass ... } } Is there a way to get the type of the enclosing class ( Impossible in this case) without writing the name of the class in the function? The reason why I'd like to do that is to avoid repeating the class name in

Why can't you call a non-static method from a static method?

雨燕双飞 提交于 2019-11-26 17:06:16
问题 I have a static method [Method1] in my class, which calls another method [Method2] in the same class and is not a static method. But this is a no-no. I get this error: An object reference is required for the non-static field, method, or property "ClassName.MethodName()" Can someone please briefly describe why? including other things that might be related to this. 回答1: A non-static method requires an instance of the class. Unless you have passed in an instance, or created an instance in your

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

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 16:58:26
问题 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? 回答1: As Baba already

Class with single method — best approach?

一世执手 提交于 2019-11-26 16:51:49
Say I have a class that's meant to perform a single function. After performing the function, it can be destroyed. Is there any reason to prefer one of these approaches? // Initialize arguments in constructor MyClass myObject = new MyClass(arg1, arg2, arg3); myObject.myMethod(); // Pass arguments to method MyClass myObject = new MyClass(); myObject.myMethod(arg1, arg2, arg3); // Pass arguments to static method MyClass.myMethod(arg1, arg2, arg3); I was being intentionally vague about the details, to try to get guidelines for different situations. But I didn't really have in mind simple library

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

倾然丶 夕夏残阳落幕 提交于 2019-11-26 16:31:22
问题 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

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

六月ゝ 毕业季﹏ 提交于 2019-11-26 15:52:32
问题 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? 回答1: The

Static method behavior in multi-threaded environment in java

时光毁灭记忆、已成空白 提交于 2019-11-26 14:59:07
问题 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

Inheritance in Static Methods

喜你入骨 提交于 2019-11-26 14:30:56
问题 Why does the below code print "Main"? public class Main { public static void method() { System.out.println("Main"); } public static void main(String[] args) { Main m = new SubMain(); m.method(); } } class SubMain extends Main { public static void method() { System.out.println("SubMain"); } } At runtime, m is pointing to an instance of Submain , so it should conceptually print "SubMain". 回答1: Static methods are resolved on the compile-time type of the variable. m is of type Main , so the

.NET: Determine the type of “this” class in its static method

北战南征 提交于 2019-11-26 14:28:05
In a non-static method I could use this.GetType() and it would return the Type . How can I get the same Type in a static method? Of course, I can't just write typeof(ThisTypeName) because ThisTypeName is known only in runtime. Thanks! If you're looking for a 1 liner that is equivalent to this.GetType() for static methods, try the following. Type t = MethodBase.GetCurrentMethod().DeclaringType Although this is likely much more expensive than just using typeof(TheTypeName) . Jon Skeet There's something that the other answers haven't quite clarified, and which is relevant to your idea of the type