static-methods

Accessing SharedPreferences through static methods

北战南征 提交于 2019-11-26 03:38:06
问题 I have some information stored as SharedPreferences. I need to access that information from outsite an Activity (in from a domain model class). So I created a static method in an Activity which I only use to get the shared preferences. This is giving me some problems, since apparently it is not possible to call the method \"getSharedPreferences\" from a static method. Here\'s the message eclipse is giving me: Cannot make a static reference to the non-static method getSharedPreferences(String,

Namespace + functions versus static methods on a class

余生颓废 提交于 2019-11-26 03:19:43
问题 Let\'s say I have, or am going to write, a set of related functions. Let\'s say they\'re math-related. Organizationally, should I: Write these functions and put them in my MyMath namespace and refer to them via MyMath::XYZ() Create a class called MyMath and make these methods static and refer to the similarly MyMath::XYZ() Why would I choose one over the other as a means of organizing my software? 回答1: By default, use namespaced functions. Classes are to build objects, not to replace

What's the difference between “class method” and “static method”?

为君一笑 提交于 2019-11-26 02:34:13
问题 I\'ve worked with a few different languages such as Java, C#, and Objective-C. In most languages, methods that don\'t require an instance of an object are called static methods. However, when it comes to Objective-C, some people get defensive when you call them static methods, and they expect you to call them class methods. Why are they called class methods instead of static methods? What is the difference between a static method and a class method? 回答1: So my question is why are they called

Method overloading and choosing the most specific type

爷,独闯天下 提交于 2019-11-26 02:06:49
The sample code is : public class OverloadingTest { public static void test(Object obj){ System.out.println("Object called"); } public static void test(String obj){ System.out.println("String called"); } public static void main(String[] args){ test(null); System.out.println("10%2==0 is "+(10%2==0)); test((10%2==0)?null:new Object()); test((10%2==0)?null:null); } And the output is : String called 10%2==0 is true Object called String called The first call to test(null) invokes the method with String argument , which is understandable according to The Java Language Specification . 1) Can anyone

Method can be made static, but should it?

耗尽温柔 提交于 2019-11-26 01:28:58
问题 Resharper likes to point out multiple functions per asp.net page that could be made static. Does it help me if I do make them static? Should I make them static and move them to a utility class? 回答1: Static methods versus Instance methods 10.2.5 Static and instance members of the C# Language Specification explains the difference. Generally, static methods can provide a very small performance enhancement over instance methods, but only in somewhat extreme situations (see this answer for some

Method overloading and choosing the most specific type

烈酒焚心 提交于 2019-11-26 01:08:49
问题 The sample code is : public class OverloadingTest { public static void test(Object obj){ System.out.println(\"Object called\"); } public static void test(String obj){ System.out.println(\"String called\"); } public static void main(String[] args){ test(null); System.out.println(\"10%2==0 is \"+(10%2==0)); test((10%2==0)?null:new Object()); test((10%2==0)?null:null); } And the output is : String called 10%2==0 is true Object called String called The first call to test(null) invokes the method

Static method in a generic class?

孤街浪徒 提交于 2019-11-26 00:28:29
问题 In Java, I\'d like to have something as: class Clazz<T> { static void doIt(T object) { // shake that booty } } But I get Cannot make a static reference to the non-static type T I don\'t understand generics beyond the basic uses and thus can\'t make much sense of that. It doesn\'t help that I wasn\'t able to find much info on the internet about the subject. Could someone clarify if such use is possible, by a similar manner? Also, why was my original attempt unsuccessful? 回答1: You can't use a

static variable link error

放肆的年华 提交于 2019-11-25 23:37:54
问题 I\'m writing C++ code on a mac. Why do I get this error when compiling?: Undefined symbols for architecture i386: \"Log::theString\", referenced from: Log::method(std::string) in libTest.a(Log.o) ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation) Not sure if my code is wrong or I have to add additional flags to Xcode. My current XCode configurations are the default ones for a \'static library\' project. My code: Log.h-

Why can&#39;t I define a static method in a Java interface?

微笑、不失礼 提交于 2019-11-25 23:12:07
问题 EDIT: As of Java 8, static methods are now allowed in interfaces. Here\'s the example: public interface IXMLizable<T> { static T newInstanceFromXML(Element e); Element toXMLElement(); } Of course this won\'t work. But why not? One of the possible issues would be, what happens when you call: IXMLizable.newInstanceFromXML(e); In this case, I think it should just call an empty method (i.e. {}). All subclasses would be forced to implement the static method, so they\'d all be fine when calling the

Difference between Static methods and Instance methods

淺唱寂寞╮ 提交于 2019-11-25 22:48:37
问题 I was just reading over the text given to me in my textbook and I\'m not really sure I understand what it is saying. It\'s basically telling me that static methods or class methods include the \"modifier\" keyword static. But I don\'t really know what that means? Could someone please explain to me in really simple terms what Static or Class Methods are? Also, could I get a simple explanation on what Instance methods are? This is what they give me in the textbook: There are important practical