instantiation

How to instantiate an object in java?

余生颓废 提交于 2019-12-17 10:55:10
问题 I'm new in programming and I would like to know where did I go wrong in instantiating an object. Below is the code: public class Testing{ private int Sample(int c) { int a = 1; int b = 2; c = a + b; return c; } public static void main(String []args) { Sample myTest = new Sample(); System.out.println(c); } } 回答1: There is no Sample class in your code . The one which you have declared is a private method . // private method which takes an int as parameter and returns another int private int

Pattern for lazy thread-safe singleton instantiation in java

試著忘記壹切 提交于 2019-12-17 10:35:14
问题 the lazy thread-safe singleton instantion is kinda not easy to understand to every coder, so i wanted to create a class in our enterprise framework that would do the job. What do you think about it? Do you see something bad about it? Is there something similar like in Apache Commons? How can i make it better? Supplier.java public interface Supplier<T> { public T get(); } LazyThreadSafeInstantiator.java public class LazyThreadSafeInstantiator<T> implements Supplier<T> { private final Supplier

C# Delegate Instantiation vs. Just Passing the Method Reference

会有一股神秘感。 提交于 2019-12-17 09:57:15
问题 I have a simple question: what's the advantage of instantiating a C# delegate as opposed to just passing the function reference? What I mean is: Why do: Thread t = new Thread(new ThreadStart(SomeObject.SomeMethod)); When you can do: Thread t = new Thread(SomeObject.SomeMethod); Both will compile and work in my experience...am I missing something? 回答1: As long as the method group SomeObject.SomeMethod has a method with return type void and taking no parameters there is no difference. This is

C# Delegate Instantiation vs. Just Passing the Method Reference

拟墨画扇 提交于 2019-12-17 09:57:02
问题 I have a simple question: what's the advantage of instantiating a C# delegate as opposed to just passing the function reference? What I mean is: Why do: Thread t = new Thread(new ThreadStart(SomeObject.SomeMethod)); When you can do: Thread t = new Thread(SomeObject.SomeMethod); Both will compile and work in my experience...am I missing something? 回答1: As long as the method group SomeObject.SomeMethod has a method with return type void and taking no parameters there is no difference. This is

Why is [] faster than list()?

大憨熊 提交于 2019-12-17 01:19:51
问题 I recently compared the processing speeds of [] and list() and was surprised to discover that [] runs more than three times faster than list() . I ran the same test with {} and dict() and the results were practically identical: [] and {} both took around 0.128sec / million cycles, while list() and dict() took roughly 0.428sec / million cycles each. Why is this? Do [] and {} (and probably () and '' , too) immediately pass back a copies of some empty stock literal while their explicitly-named

How is an instance initializer different from a constructor?

五迷三道 提交于 2019-12-16 23:58:32
问题 In other words, why would you need an instance initializer? What difference or advantage do you have in writing a instance initializer over a constructor? 回答1: This seems to explain it well: Instance initializers are a useful alternative to instance variable initializers whenever: initializer code must catch exceptions, or perform fancy calculations that can't be expressed with an instance variable initializer. You could, of course, always write such code in constructors. But in a class that

Weird value referencing

▼魔方 西西 提交于 2019-12-14 02:36:24
问题 I faced a problem today which has happened to me several times before. I have a generic list of my object: List<Classes.Object.GameObject> ObjectList = new List<Classes.Object.GameObject>(); I decided to get an instance of Classes.Object.GameObject from the list by: Classes.Object.GameObject TempObject = new Classes.Object.GameObject; TempObject = ObjectList[10]; It's working good till here but when I do some changes on TempObject the object of index in list which I mentioned changes too! I

Visual Studio WinForms designer does not instantiate object

落花浮王杯 提交于 2019-12-13 12:29:52
问题 I created a class derived from System.Windows.Forms.ContextMenuStrip class, not as a user control, just a plain .cs class with a constructor and one event handler. When I drag this class from the Toolbox onto the designer, it creates a private member for it and a couple of properties, but does not instantiate an object. Thus, at runtime I get "Object reference not set to an instance of an object.", because the designer never generates the line: this.searchGridContextMenu1 = new

When is required instantiation of function template definition?

北慕城南 提交于 2019-12-13 10:01:35
问题 After I read the answer for this question, I am still asking question. The answer talks about location of point of instantiation as defined in [temp.point] but it is not evaluated if the instantiations are required. [temp.inst] specifies when a function template specialization instantiation is required, the general rule is: An implementation shall not implicitly instantiate a function template, a variable template, a member template, a non-virtual member function, a member class,[...], unless

Multidimensional arrays as class member with arbitrary bounds

烂漫一生 提交于 2019-12-13 07:15:39
问题 Not a duplicate of C++: multidimensional array initialization in constructor since the answers all appear to assume the bounds are known at compile-time. I'm making a weighted undirected graph class arraygraph , backed by a 2D array of int , by the name of edges[][] . At instantiation time I don't really care what edges[][] holds; arraygraph has a method that reads a graph from a given filename and edges is set to a new int[n][n] (where n is the # of nodes in the file) by that function before