instantiation

Instantiating object from inside the main of that class in Java

半城伤御伤魂 提交于 2019-11-28 07:33:52
I was looking through my OOP class documentation and I found this example: class Student { private String name; public int averageGrade; public Student(String n, int avg) { name = n; averageGrade = avg; } public static void main(String[] args) { Student s = new Student("John", 9); } } I find it confusing that they are instantiating an object from the main of the same class. Is this considered bad practice? Will the newly created object s have a main method? Thank you! There's nothing wrong at all with this. It's entirely normal. (Admittedly it would make more sense for a class with a main

Generics and Class.forName

ⅰ亾dé卋堺 提交于 2019-11-28 07:25:59
I would like to create an instance of a specified class using its name. My code is shown below. I get a compiler warning. Am I doing this the right way? Is it even possible to use the name of a class and get an instance of that type back, as I don't think there is any way of the compiler knowing what the type should be? public static <T> T create(final String className) { try { final Class<?> clazz = Class.forName(className); //WARNING: Type safety: Unchecked cast from capture#2-of ? to T return (T) create(clazz); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static <T>

How should I select which concrete implementation should be instantiated based on the user choice?

旧城冷巷雨未停 提交于 2019-11-28 06:27:55
问题 I have an interface Fruit with two implementations Apple and Banana . I want to create a Fruit instance. The choice whether the concrete implementation should be an Apple or a Banana should be made by the user. I did not yet design the user interface, so there is no restriction how this choice is made by the user. I know there are the following options: usage of the abstract factory pattern usage of reflection to create an instance from a given class name usage of reflection to create an

How to instantiate the class in an assembly using Reflection with C#/.NET?

拈花ヽ惹草 提交于 2019-11-28 06:04:57
问题 I have this library to compiled to calc.dll. namespace MyClass { public class Calculator { public int Value1 {get; set;} public int Value2 {get; set;} public Calculator() { Value1 = 100; Value2 = 200; } public int Add(int val1, int val2) { Value1 = val1; Value2 = val2; return Value1 + Value2; } } } I want to instantiate the Calculate class without linking to the calc.dll. Can C# do that? I came up with this code, but I don't know how to instantiate the Calculator class. using System; using

Why do I get an error instantiating an interface?

梦想与她 提交于 2019-11-28 05:15:01
I have a class and an interface, and when I try to instantiate the interface, I get an error: Cannot create an instance of the abstract class or interface My code is below: namespace MyNamespace { public interface IUser { int Property1 { get; set; } string Property2 { get; set; } string Property3 { get; set; } void GetUser(); } public class User : IUser { public int Property1 { get; set; } public string Property2 { get; set; } public string Property3 { get; set; } public void GetUser() { //some logic here...... } } } When I try to instantiate IUser user = new IUser(); I get an error: Cannot

abstract class NumberFormat - very confused about getInstance()

那年仲夏 提交于 2019-11-28 04:43:17
问题 I'm new to Java and I have a beginner question: NumberFormat is an abstract class and so I assume I can't make an instance of it. But there is a public static (factory?) method getInstance() that allow me to do NumberFormat nf = NumberFormat.getInstance(); I'm quite confuse. I'll be glad if someone could give me hints on: If there is a public method to get an instance of this abstract class, why don't we have also a constructor? This is an abstract class ; how can we have this static method

Why is it possible to instantiate a struct without the new keyword?

放肆的年华 提交于 2019-11-28 03:43:00
Why are we not forced to instantiate a struct, like when using a class? The why is simply - because the spec says so . The how is a matter of ensuring that the entire block of memory is "definitely assigned", which means: assigning a value to each field of the struct. However, this requires 2 nasty things: public fields (almost always bad) mutable fields (generally bad in a struct) so in most best-practice cases , you do need to use the new(...) syntax, to invoke the constructor (or to zero-the memory, for the parameterless constructor) for the type correctly. Eric Lippert Why are we not

cannot instantiate a class using a button

人走茶凉 提交于 2019-11-28 00:29:30
I am trying to make a screen capturing program. What I have is a transparent window, which will give the area to be captured, with a button capture on it, and I am trying to instantiate a class captureScreen that works good when captureScreen is individually executed using a command prompt I am trying to instantiate this captureScreen class when button capture is hit. I have tried keeping this class on my screenrecord.java , putting the code in event listener also. In both these cases,I get these errors AWTException,must be caught or declared in Robot robot = new Robot(); and IOException in

class template instantiation

耗尽温柔 提交于 2019-11-27 22:55:16
I just read the wiki article about CRTP , and I'm a little confused about template instantiation. According to the wiki, member function bodies (definitions) are not instantiated until long after their declarations. I don't quite understand what it means. Suppose I got a class template: template <typename T> class A { public: void foo(T t) { //... }; }; When I instantiate the class template A, does it instantiate the member function foo()? For example: //in .cpp file int main() { A<int> a; //question 1 //class template is instantiated here, isn't it? //What about foo(), is it instantiated too?

Does System.Activator.CreateInstance(T) have performance issues big enough to discourage us from using it casually?

假装没事ソ 提交于 2019-11-27 19:40:36
Does System.Activator.CreateInstance(T) method have performance issues (since I'm suspecting it uses reflection) big enough to discourage us from using it casually? As always, the only correct way to answer a question about performance is to actually measure the code. Here's a sample LINQPad program that tests: Activator.CreateInstance new T() calling a delegate that calls new T() As always, take the performance program with a grain of salt, there might be bugs here that skews the results. The output (timing values are in milliseconds): Test1 - Activator.CreateInstance<T>() 12342 Test2 - new T