instance

Is calling static methods via an object “bad form”? Why?

一曲冷凌霜 提交于 2019-11-26 15:28:32
In a recent question, someone asked about static methods and one of the answers stated that you generally call them with something like: MyClassName.myStaticMethod(); The comments on that also stated that you could also call it via an object with: MyClassName myVar; myVar.myStaticMethod(); but that it was considered bad form. Now it seems to me that doing this can actually make my life easier so I don't have to worry about what's static or not (a) . Is there some problem with calling static functions via an object? Obviously you wouldn't want to create a brand new object just to call it:

Can we create an instance of an interface in Java? [duplicate]

百般思念 提交于 2019-11-26 14:56:51
This question already has an answer here: Can we create an object of an interface? 5 answers Is it possible to create an instance of an interface in Java? Somewhere I have read that using inner anonymous class we can do it as shown below: interface Test { public void wish(); } class Main { public static void main(String[] args) { Test t=new Test() { public void wish() { System.out.println("output: hello how r u"); } }; t.wish(); } } cmd> javac Main.java cmd> java Main output: hello how r u Is it correct here? Magnus Yes, your example is correct. Anonymous classes can implement interfaces, and

Why am I getting, “Object reference not set to an instance of an object.” but no line of code is implicated?

最后都变了- 提交于 2019-11-26 14:50:35
问题 When I try to build my VB.NET solution, I get the error, "Object reference not set to an instance of an object." but in the Error List pane, the File, Line, Column, and Project columns are blank: How can I solve the problem if it won't tell me where it is? UPDATE I selected the Project, then "Show Project Dependency Diagram" and got this: I don't know what I'm looking at. UPDATE 2 I don't know if this is unusual or to be expected, but if I mash F5, I do get a page in a browser (http:/

C# : how to - single instance application that accepts new parameters?

为君一笑 提交于 2019-11-26 13:59:01
问题 I'm creating a (C#) program that downloads binaries using NZB files, there may only be one instance of my application running at any time. So when a user doubleclicks an .nzb-file and my program is not running, it should start and process it (easy, file registration). Now if my program is already running, I do NOT want to launch a second instance - I want the already-running instance to pick up the specified file. Making my app single-instance can be done using the Visual Basic DLL with the

Swift: Creating an Array with a Default Value of distinct object instances

ⅰ亾dé卋堺 提交于 2019-11-26 13:46:50
I noticed a bit weird ( and dangerous IMHO ) behavoir in Creating an Array with a Default Value . As stated in Swift 2.1: Collection Types Swift’s Array type also provides an initializer for creating an array of a certain size with all of its values set to the same default value. You pass this initializer the number of items to be added to the new array (called count) and a default value of the appropriate type (called repeatedValue): The point is: same default value ; in order to understand how it work, I tried to create an array of elements of this example class class User { private struct

Why is the System.Random class not static?

拈花ヽ惹草 提交于 2019-11-26 12:47:01
问题 When you use the System.Random class, you must make an instance of it. Why is it not static ? Because if I want a random number between 0 and 9, I can use the static method , System.Random.Next(int, int) : int ourRandomNumber = Random.Next(0,9); So why isn\'t the class just static ? 回答1: You wouldn't be able to use different seeds if it were static - the Random instance keeps track of that state. By default Random uses the current time as seed, but re-using a particular seed (i.e. new Random

Ruby on Rails - Access controller variable from model

不问归期 提交于 2019-11-26 12:44:27
问题 I am trying to access an instance variable which is set in the controller in the model. The controller is the products controller and the model is the products model. The instance variable is a instance of another model called account. The instance variable is @current_account When I run the code nothing happens, I do not get an error. Does anyone know where I can find something read about access instance variables set in the controller from the model? Thanks Eef 回答1: You shouldn't generally

python class instance variables and class variables

自作多情 提交于 2019-11-26 12:23:38
I'm having a problem understanding how class / instance variables work in Python. I don't understand why when I try this code the list variable seems to be a class variable class testClass(): list = [] def __init__(self): self.list.append('thing') p = testClass() print p.list f = testClass() print f.list Output: ['thing'] ['thing', 'thing'] and when I do this it seems to be an instance variable class testClass(): def __init__(self): self.list = [] self.list.append('thing') p = testClass() print p.list f = testClass() print f.list Output: ['thing'] ['thing'] rodrigo This is because of the way

Why won't dynamically adding a `__call__` method to an instance work?

淺唱寂寞╮ 提交于 2019-11-26 12:19:17
问题 In both Python 2 and Python 3 the code: class Foo(object): pass f = Foo() f.__call__ = lambda *args : args f(1, 2, 3) returns as error Foo object is not callable . Why does that happen? PS: With old-style classes it works as expected. PPS: This behavior is intended (see accepted answer). As a work-around it\'s possible to define a __call__ at class level that just forwards to another member and set this \"normal\" member to a per-instance __call__ implementation. 回答1: Double-underscore

Non-Singleton Services in AngularJS

佐手、 提交于 2019-11-26 11:48:37
问题 AngularJS clearly states in its documentation that Services are Singletons: AngularJS services are singletons Counterintuitively, module.factory also returns a Singleton instance. Given that there are plenty of use-cases for non-singleton services, what is the best way to implement the factory method to return instances of a Service, so that each time an ExampleService dependency is declared, it is satisfied by a different instance of ExampleService ? 回答1: I don't think we should ever have a