static-methods

Java: Calling a static method in the main() method

左心房为你撑大大i 提交于 2019-11-30 06:03:58
问题 I am supposed to do the following: Write a Java application (Client) program with a static method called generateEmployees( ) that returns a random list of 10 different types of Employee objects. You could either use an array or an ArrayList to store the employee objects that will be returned. Use a for loop to populate randomly different types of employee objects with some random data. You could possibly think a range of values like 1 – 4. If random value is 1, create a HourlyEmployee object

What is difference between extension method and static method?

我们两清 提交于 2019-11-30 04:55:07
问题 What is the difference between an extension method and a static method ? I have two classes like this : public static class AClass { public static int AMethod(string ....) { } } and public static class BClass { public static int BMethod(this string ....) { } } I can use these like AClass.AMethod('...'); or '...'.BMethod(); Which is proposed ? 回答1: An extension method is still a static method. You can use it exactly as you'd use a normal static method. The only difference is that an extension

Static method cannot access instance members of a class

不羁岁月 提交于 2019-11-30 04:45:29
问题 In Liang's 9th edition Introduction to Java Programming it states, "A static method cannot access instance members of a class," (pg 312). I see why an instance member of a class would need to access a method (which might be static), but why would a method need to access an instance member? To me, "access" means "access by way of the dot operator." In other words: Class myClass = new Class(); myClass.someStaticMethod(); makes sense, whereas: someNonStaticMethod.myClass or someStaticMethod

Using Static method and variables - Good vs Bad

核能气质少年 提交于 2019-11-30 03:51:50
问题 I am developing C# and asp.net web application. I have general class called utilities, I have lot of public and static variables in this public utilities class. Since this number is gradually increasing, I want to know is it good practice to store utilities methods and variable as public static. Example of my code public class utilities { public static string utilVariable1 = "Myvalue"; public static string utilVariable2 = "Myvalue"; public static string utilVariable3 = "Myvalue"; : public

How garbage collection works on object references?

久未见 提交于 2019-11-30 03:12:35
问题 I am confused about garbage collection process on objects. object A = new object(); object B = A; B.Dispose(); By calling a Dispose on variable B only, the created object will not be garbage collected as the object is still has referenced by A. Now does the following code work same as above? public static image Test1() { Bitmap A = new Bitmap(); return A; } Now I call this static function from some other method. public void TestB() { Bitmap B = Test1(); B.Dispose(); } The static function

“Expected class, delegate, enum, interface or struct” error on public static string MyFunc(). What's an alternative to “string”?

家住魔仙堡 提交于 2019-11-30 01:45:49
问题 I'm getting an error when I attempt to use the following static function. Error: Expected class, delegate, enum, interface, or struct Function (and class): namespace MyNamespace { public class MyClass { // Some other static methods that use Classes, delegates, enums, interfaces, or structs public static string MyFunc(string myVar){ string myText = myVar; //Do some stuff with myText and myVar return myText; } } } This is causing the compiler to angrily (in red) underline the string part of

Call static method from instance in PHP, future deprecation?

泄露秘密 提交于 2019-11-30 01:08:51
问题 While I understand the $this variable is not available when a method is called in a static context, to assist in decoupling my application components from one-another I figured it would make sense to call static methods from an instance. For example: class MyExample{ private static $_data = array(); public static function setData($key, $value){ self::$_data[$key] = $value; } // other non-static methods, using self::$_data } // to decouple, another class or something has been passed an

Static Vs Instance Method Performance C#

你。 提交于 2019-11-30 01:07:39
I have few global methods declared in public class in my ASP.NET web application. I have habit of declaring all global methods in public class in following format public static string MethodName(parameters) { } I want to know how it would impact on performance point of view? Which one is better? Static Method or Non-Static Method? Reason why it is better? http://bytes.com/topic/c-sharp/answers/231701-static-vs-non-static-function-performance#post947244 states: because, static methods are using locks to be Thread-safe. The always do internally a Monitor.Enter() and Monitor.exit() to ensure

Static function declared but not defined in C++

£可爱£侵袭症+ 提交于 2019-11-29 23:28:47
I'm getting an error from the following code using C++. Main.cpp #include "file.h" int main() { int k = GetInteger(); return 0; } File.h static int GetInteger(); File.cpp #include "file.h" static int GetInteger() { return 1; } The error I get: Error C2129: static function 'int GetInteger(void)' declared but not defined. I've read the famous article "Organizing Code File in C and C++" , but don't understand what is wrong with this code. In C++, static at global/namespace scope means the function/variable is only used in the translation unit where it is defined, not in other translation units.

`staticmethod` and `abc.abstractmethod`: Will it blend?

半世苍凉 提交于 2019-11-29 22:00:00
In my Python app I want to make a method that is both a staticmethod and an abc.abstractmethod . How do I do this? I tried applying both decorators, but it doesn't work. If I do this: import abc class C(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod @staticmethod def my_function(): pass I get an exception*, and if I do this: class C(object): __metaclass__ = abc.ABCMeta @staticmethod @abc.abstractmethod def my_function(): pass The abstract method is not enforced. How can I make an abstract static method? *The exception: File "c:\Python26\Lib\abc.py", line 29, in abstractmethod funcobj