instantiation

How to instantiate an object in java?

放肆的年华 提交于 2019-11-27 13:45:37
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); } } 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 Sample(int c) { int a = 1; int b = 2; c = a + b; return c; } With the current snippet , You need to instantiate

Pattern for lazy thread-safe singleton instantiation in java

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 11:58:40
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<T> instanceSupplier; private volatile T obj; public LazyThreadSafeInstantiator(Supplier<T>

How do I force a particular instance of a C++ template to instantiate?

依然范特西╮ 提交于 2019-11-27 11:33:13
See title. I have a template. I want to force a particular instance of a template to instantiate. How do I do this? More specifically, can you force an abstract template class to instantiate? I might elaborate as I have the same question. In my case I am building a library, some of the template implementations are large and include lots of stuff, but are only generated for a couple of types. I want to compile them in the library and export all the methods, but not include the header with the code everywhere. ie: template<class T> OS_EXPORT_DECL class MyTmpl { T *item1; public: inline T

How to Properly Declare Array of Custom Objects in Swift?

拟墨画扇 提交于 2019-11-27 11:19:52
问题 Here is my custom class...not sure if I'm missing anything in it... import UIKit class baseMakeUp { var Image = UIImage() var Brand: String var Color: String var Rating: Int = 0 init (Brand: String, Color: String) { self.Brand = Brand self.Color = Color } } I'm trying to instantiate here... import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } let

base() and this() constructors best practices

流过昼夜 提交于 2019-11-27 10:31:17
Under what conditions am I supposed to make the :base() and :this() constructor calls following my constructor's parentheses (or even in other places in the code). When are these calls good practices and when are they mandatory? Mark Byers : base(...) If you omit the call to a base constructor it will call the default base constructor automatically. It is mandatory to call a base constructor explicitly if there is no default constructor. Even if there is a default constructor you may still wish to call a different constructor than the default constructor. In this case you may still wish to use

C# Delegate Instantiation vs. Just Passing the Method Reference

狂风中的少年 提交于 2019-11-27 09:29: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? 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 because ThreadStart is defined as a delegate that returns void and takes no parameters and therefore there is

Java - Interface, instantiating an interface?

夙愿已清 提交于 2019-11-27 06:58:08
问题 So I just found this code example online a while ago and I'm going over it again but quite confused. From looking at it, what I gather (and it might be wrong) is that it passes to the print method in the NumberPrinter class a Printer object. However, the interface is also called Printer, so aren't we instantiating an anonymous class of the Printer interface, defining the methods and then passing it? My basic question is, is my initial assumption correct? And if so I thought you could not

Is it possible to “dynamically” create local variables in Python? [duplicate]

依然范特西╮ 提交于 2019-11-27 06:49:30
问题 This question already has an answer here: Dynamically set local variable [duplicate] 7 answers Is it possible to create a local variables with Python code, given only the variable's name (a string), so that subsequent calls to "'xxx' in locals()" will return True? Here's a visual : >>> 'iWantAVariableWithThisName' in locals() False >>> junkVar = 'iWantAVariableWithThisName' >>> (...some magical code...) >>> 'iWantAVariableWithThisName' in locals() True For what purpose I require this trickery

Is there a way to instantiate a class without calling __init__?

痞子三分冷 提交于 2019-11-27 06:47:37
Is there a way to circumvent the constructor __init__ of a class in python? Example: class A(object): def __init__(self): print "FAILURE" def Print(self): print "YEHAA" Now I would like to create an instance of A . It could look like this, however this syntax is not correct. a = A a.Print() EDIT: An even more complex example: Suppose I have an object C , which purpose it is to store one single parameter and do some computations with it. The parameter, however, is not passed as such but it is embedded in a huge parameter file. It could look something like this: class C(object): def __init__

C++ Object Instantiation

老子叫甜甜 提交于 2019-11-27 05:47:41
I'm a C programmer trying to understand C++. Many tutorials demonstrate object instantiation using a snippet such as: Dog* sparky = new Dog(); which implies that later on you'll do: delete sparky; which makes sense. Now, in the case when dynamic memory allocation is unnecessary, is there any reason to use the above instead of Dog sparky; and let the destructor be called once sparky goes out of scope? Thanks! jalf On the contrary, you should always prefer stack allocations, to the extent that as a rule of thumb, you should never have new/delete in your user code. As you say, when the variable