instantiation

Create Annotation instance with defaults, in Java

无人久伴 提交于 2019-11-27 19:00:31
How can I create an instance of the following annotation (with all fields set to their default value). @Retention( RetentionPolicy.RUNTIME ) public @interface Settings { String a() default "AAA"; String b() default "BBB"; String c() default "CCC"; } I tried new Settings() , but that does not seem to work... Ralph To create an instance you need to create a class that implements: java.lang.Annotation and the annotation you want to "simulate" For example: public class MySettings implements Annotation, Settings But you need to pay special attention to the correct implementation of equals and

Avoid instantiating a class in java

*爱你&永不变心* 提交于 2019-11-27 18:36:10
Recently I've faced a question : How to avoid instantiating a Java class? However, I answered by saying: If you don't want to instantiate a class, use "abstract" modifier. Ex: javax.servlet.HttpServlet, is declared as abstract(though none of its methods are abstract) to avoid instantiation. Declare a no argument private constructor. Now my question is a) are there any other ways? b) why does any one do not want to instantiate a class? - after searching in SO, I got to know from this that Util classes can be made not to instantiate. Any other places where we don't want to instantiate a class in

What's the difference between dict() and {}?

被刻印的时光 ゝ 提交于 2019-11-27 17:14:40
So let's say I wanna make a dictionary. We'll call it d . But there are multiple ways to initialize a dictionary in Python! For example, I could do this: d = {'hash': 'bang', 'slash': 'dot'} Or I could do this: d = dict(hash='bang', slash='dot') Or this, curiously: d = dict({'hash': 'bang', 'slash': 'dot'}) Or this: d = dict([['hash', 'bang'], ['slash', 'dot']]) And a whole other multitude of ways with the dict() function. So obviously one of the things dict() provides is flexibility in syntax and initialization. But that's not what I'm asking about. Say I were to make d just an empty

Why can private member variable be changed by class instance?

白昼怎懂夜的黑 提交于 2019-11-27 17:12:57
问题 class TestClass { private string _privateString = "hello"; void ChangeData() { TestClass otherTestClass = new TestClass(); otherTestClass._privateString = "world"; } } This code compiles in C# and the equivalent works in PHP, but can someone explain the reason why otherTestClass._privateString can be changed here ? I would have thought an instance of a class should not be able to change a private member variable under any circumstances, and that trying to access otherTestClass._privateString

calling class method (with constructors) without object instantiation in php

怎甘沉沦 提交于 2019-11-27 16:46:45
问题 Ive looked and tried but I can't find an answer. In PHP, is it possible to call a class' member function (when that class requires a constructor to receive parameters) without instantiating it as an object? A code example (which gives errors): <?php class Test { private $end=""; function __construct($value) { $this->end=$value; } public function alert($value) { echo $value." ".$this->end; } } //this works: $example=new Test("world"); $example->alert("hello"); //this does not work: echo Test(

What makes my.class.js so fast? [closed]

£可爱£侵袭症+ 提交于 2019-11-27 16:32:35
I've been looking at the source code of my.class.js to find out what makes it so fast on Firefox. Here's the snippet of code used to create a class: my.Class = function () { var len = arguments.length; var body = arguments[len - 1]; var SuperClass = len > 1 ? arguments[0] : null; var hasImplementClasses = len > 2; var Class, SuperClassEmpty; if (body.constructor === Object) { Class = function () {}; } else { Class = body.constructor; delete body.constructor; } if (SuperClass) { SuperClassEmpty = function() {}; SuperClassEmpty.prototype = SuperClass.prototype; Class.prototype = new

What determines when a class object is destroyed in PHP?

我是研究僧i 提交于 2019-11-27 15:59:05
问题 Let's say that we have class CFoo . In the following example when is CFoo::__destruct() called? function MyPHPFunc() { $foo = new CFoo(); . . . // When/where/how does $foo get destroyed/deleted? } In this example would the destructor be called when the script exits the scope of MyPHPFunc because $foo would no longer be accessible? 回答1: In PHP all values are saved in so called zval s. Those zval s contain the actual data, type information and - this is important for your question - a reference

Get Enum Instance from Class<? extends Enum> using String value?

陌路散爱 提交于 2019-11-27 15:39:56
问题 I'm finding it difficult to put the exact question into words, so I'll just give an example. I have two Enum types: enum Shape { CAT, DOG; } enum Color { BLUE, RED; } I have a method: public Object getInstance(String value, Class<?> type); I would like to use the method like: // someValue is probably "RED", and someEnumClass is probably Color.class Color c = getInstance(someValue, someEnumClass); I've been having trouble determining exactly how to implement getInstance() . Once you know the

Create a new instance of T without the new constraint

混江龙づ霸主 提交于 2019-11-27 15:05:27
问题 If one wants to create a new instance of a generic, the new constraint needs to be defined, like so: public T SomeMethod<T>() where T : new() { return new T(); } Is it possible, using reflection, to create an instance of T without the new constraint, like so (contains pseudocode): public T SomeMethod<T>() { if (T has a default constructor) { return a new instance of T; } else { return Factory<T>.CreateNew(); } } 回答1: Use Activator.CreateInstance() for this. See http://msdn.microsoft.com/en-us

How does one instantiate an array of maps in Java?

梦想与她 提交于 2019-11-27 13:53:06
I can declare an array of maps using generics to specify the map type: private Map<String, Integer>[] myMaps; However, I can't figure out how to instantiate it properly: myMaps = new HashMap<String, Integer>[count]; // gives "generic array creation" error myMaps = new HashMap[count]; // gives an "unchecked or unsafe operation" warning myMaps = (Map<String, Integer>[])new HashMap[count]; // also gives warning How can I instantiate this array of maps without getting a compiler error or warning? Update: Thank you all for your replies. I ended up going with the List suggestion. Not strictly an