instantiation

abstract class NumberFormat - very confused about getInstance()

若如初见. 提交于 2019-11-29 11:07:42
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 giving us an instance of the class? Why choosing such a design? If I assume it's possible to have an

PMD: Avoid instantiating new objects inside loops

假装没事ソ 提交于 2019-11-29 11:07:00
问题 I've got an issue with the PMD rule Avoid instantiating new objects inside loops . Here is some example code: import java.awt.Dimension; public class PMDDemo { public static void main(final String[] args) { final Dimension[] arr = new Dimension[10]; for (int i = 0; i < arr.length; i++) { arr[i] = new Dimension(i, i); // rule violation here } } } PMD gives me the above mentioned rule violation at the marked spot in the code. How am I supposed to create n instances of a class without creating

Generics wildcard instantiation

走远了吗. 提交于 2019-11-29 10:30:58
I was reviewing someone else's code the other day and I came across a line that raised some concern. To simplify, say I have a generic Class A and an abstract Class B. Is the following instantiation allowed and if so, why? Object obj = new A<? extends B>(); I personally have never seen an instantiation like the above, although a declaration such as A<? extends B> obj = null; would certainly hold. I've always used the wildcard in generics to declare method parameters, so I may just not have the experience. Actually new A<? extends B>() does not compile. It has been consistently illegal since

newInstance() with inner classes

六月ゝ 毕业季﹏ 提交于 2019-11-29 06:33:03
I've been working on an instantiation method that will allow me to package a variety of similar classes into one outer class. I could then instantiate each unique class type by passing the name of that type to the constructor. After a lot of research and errors, this is what I have come up with. I have left an error, to demonstrate my question. import java.lang.reflect.Constructor; public class NewTest { public static void main(String[] args) { try { Class toRun = Class.forName("NewTest$" + args[0]); toRun.getConstructor().newInstance(); } catch(Exception ex) { ex.printStackTrace(); System.out

deducing references to const from rvalue arguments

岁酱吖の 提交于 2019-11-29 06:22:59
Okay, this may seem like a silly question, but here it goes: template <typename T> void foo(T& x) { } int main() { foo(42); // error in passing argument 1 of 'void foo(T&) [with T = int]' } What is preventing C++ to instantiate the foo function template with T = const int instead? The problem is that template type deduction has to work out an exact match, and in that particular case, because of the reference in the signature, an exact match requires an lvalue. The value 42, is not an lvalue, but rather an rvalue, and resolving T with const int would not yield a perfect match. Since template

Why can private member variable be changed by class instance?

∥☆過路亽.° 提交于 2019-11-29 02:55:41
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 would give an 'inaccessible due to protection level' error. This is not the case though, so why does

Java: Extending Class At Runtime

丶灬走出姿态 提交于 2019-11-29 02:54:29
问题 I have the capability to extend a class at compile time, but I need to be able to create an instance of this subclass at runtime using an instance of the superclass that was already instantiated. This should be possible in theory because superclass constructors are already called before the subclass constructor. I do not have access to the program sufficiently to change the instantiation to my subclass nor to interrupt the original instantiation. Use Case: There is an existing array of

Why can private member variable be changed by class instance?

时光总嘲笑我的痴心妄想 提交于 2019-11-29 02:53: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 would give an 'inaccessible due to protection level' error. This is not the case though, so why does

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

岁酱吖の 提交于 2019-11-29 02:28:30
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("world")::alert("hello"); ?> Unfortunately PHP doesn't have support to do this, but you are a creative

What determines when a class object is destroyed in PHP?

筅森魡賤 提交于 2019-11-29 01:30:27
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? 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 count. Have a look at the following snippet: $a = new B; // $a points to zval(new B) with refcount=1 $b =