Managing constructors with many parameters in Java

In some of our projects, there's an class hierarchy that adds more parameters as it goes down the chain. At the bottom, some of the classes can have up to 30 parameters, 28 of which are just being passed into the super constructor. I'll acknowledge that using automated DI through something like Guice would be nice, but because of some technical reasons, these specific projects are cons

用Java中的许多参数管理构造函数

在我们的一些项目中,有一个类层次结构可以在链条上添加更多参数。 在底部,一些类可以有多达30个参数,其中28个只传递给超级构造函数。 我会承认,通过像Guice这样的自动化DI会很好,但是由于某些技术原因,这些特定的项目都受限于Java。 按类型按字母顺序组织参数的约定不起作用,因为如果某个类型被重构(您为参数2传入的圆现在是一个形状),它可能突然出现故障。 这个问题可能是特定的,充满“如果这是你的问题,你在

Why should I use the keyword "final" on a method parameter in Java?

I can't understand where the final keyword is really handy when it is used on method parameters. If we exclude the usage of anonymous classes, readability and intent declaration then it seems almost worthless to me. Enforcing that some data remains constant is not as strong as it seems. If the parameter is a primitive then it will have no effect since the parameter is passed to the meth

为什么我应该在Java的方法参数中使用关键字“final”?

当我在方法参数上使用final关键字时,我无法理解final关键字的真正用处。 如果我们排除匿名类的使用,可读性和意图声明,那么对我来说似乎几乎没有价值。 强化一些数据保持不变并不像看起来那么强劲。 如果参数是一个基元,那么它将不起作用,因为该参数作为一个值传递给该方法,并且改变它在该范围之外将不起作用。 如果我们通过引用传递一个参数,那么引用本身就是一个局部变量,并且如果引用从方法内部改变了,那么这

Java Pass By Value and Pass By Reference

This question already has an answer here: Is Java “pass-by-reference” or “pass-by-value”? 78 answers Java is always pass-by-value only, just that in this case, the reference of the HashMap is passed by value. The valueMap refers to the same object as the inputMap . That's why when you add a key-value pair using valueMap , it is reflected back in inputMap , as both are referring to the

Java按值传递和按引用传递

这个问题在这里已经有了答案: Java是“通过引用传递”还是“按值传递”? 78个答案 Java总是只传值 ,只是在这种情况下,HashMap的引用是按值传递的。 valueMap引用与inputMap相同的对象。 这就是为什么当你使用valueMap添加一个键值对时,它会反映回inputMap ,因为两者都指向相同的HashMap对象。 Eng.Fouad的这个答案很好地解释了这个概念。 Java是通过价值传递的。 但你的怀疑是参考,甚至参考在Java通过价值。

Why can't my Java method change a passed variable?

This question already has an answer here: Is Java “pass-by-reference” or “pass-by-value”? 78 answers References to objects are passed by value in Java so assigning to the local variable inside the method doesn't change the original variable. Only the local variable s points to a new string. It might be easier to understand with a little ASCII art. Initially you have this: ----------

为什么我的Java方法不能更改传递的变量?

这个问题在这里已经有了答案: Java是“通过引用传递”还是“按值传递”? 78个答案 对象引用通过Java中的值传递,因此分配给方法内的局部变量不会更改原始变量。 只有局部变量s指向一个新的字符串。 用一点ASCII艺术可能会更容易理解。 最初你有这样的: ------------ | nullTest | ------------ | null 当你第一次输入setNotNull方法时,你会得到s中nullTest值的副本。 在这种情况下,nullTest的值是空引用:

How does Arrays.sort() change the variable passed to it?

Possible Duplicate: Is Java pass-by-reference? I am a little confused here. How does Arrays.sort(a) modify the value of a? int[] a = {9,8,7,6,5,4,3,2,1}; Arrays.sort(a); System.out.println(Arrays.toString(a)); I thought java was pass by value... Yes, Java is pass-by-value. However, what is getting passed by value here is the reference to the array, not the array itself. Objects in Jav

Arrays.sort()如何更改传递给它的变量?

可能重复: Java是否通过引用? 我在这里有点困惑。 Arrays.sort(a)如何修改a的值? int[] a = {9,8,7,6,5,4,3,2,1}; Arrays.sort(a); System.out.println(Arrays.toString(a)); 我认为Java是通过价值... 是的,Java是通过价值传递的。 但是,在这里传递的值是对数组的引用,而不是数组本身。 Java中的对象通过引用的值传递。 所以如果你传入一个对象,它会得到一个引用的副本(如果你将该引用分配给别的东西,只

Object state does not change after method call

This question already has an answer here: Is Java “pass-by-reference” or “pass-by-value”? 78 answers Compare these two methods: private static void hello(String t) { t = "hello " + t; } private static void hello(CustomStringObject o) { o.str = "hello " + o.str; } In the first case, you're assigning a new value to t . That will have no effect on the calling code - you're j

方法调用后,对象状态不会改变

这个问题在这里已经有了答案: Java是“通过引用传递”还是“按值传递”? 78个答案 比较这两种方法: private static void hello(String t) { t = "hello " + t; } private static void hello(CustomStringObject o) { o.str = "hello " + o.str; } 在第一种情况下,你正在为t分配一个新值。 这对调用代码没有任何影响 - 您只需更改参数的值,并且所有参数都通过Java中的值传递。 在第二种情况下,您正在为o.str

Does a List object get passed by reference?

Possible Duplicate: Is Java pass-by-reference? Does a List object get passed by reference? In other words, if I pass an ArrayList (java.util.ArrayList) object to a class, will it be automatically updated when I change it? in other word: If I pass an ArrayList (java.util.ArrayList) object to a class, will it be automatically updated when I change it? Yes Does the List object passed by r

List对象是否通过引用传递?

可能重复: Java是否通过引用? List对象是否通过引用传递? 换句话说,如果我将一个ArrayList (java.util.ArrayList)对象传递给一个类,当它改变时它会自动更新吗? 换句话说:如果我将一个ArrayList(java.util.ArrayList)对象传递给一个类,当它改变时它会自动更新吗? 是 List对象是否通过引用传递? 引用的值将通过 public updateList(List<String> names){ //.. } 说明 当你调用updateList(someO

Changing the values of variables in methods, Java

This question already has an answer here: Is Java “pass-by-reference” or “pass-by-value”? 78 answers Look at Jon Skeet's article about Parameter-Passing in Java, which explains this. In short (look at his site for a more throughout explanation): Arrays are reference types. If you pass a reference that points to an array, the value of the reference is copied and assigned to the param

更改方法Java中的变量值

这个问题在这里已经有了答案: Java是“通过引用传递”还是“按值传递”? 78个答案 请看Jon Skeet关于Java中参数传递的文章,这解释了这一点。 总之(看他的网站更全面的解释): 数组是参考类型。 如果传递指向数组的引用,则会复制该引用的值并将其分配给该函数的参数。 所以参数将指向与传递的参数相同的数组。 因此,通过函数的参数对数组所做的更改将在调用函数中可见。 然而,改变参数本身(b),例如通过设置它

java

This question already has an answer here: Is Java “pass-by-reference” or “pass-by-value”? 78 answers Because when you call speak(name); , inside speak when you do name = name.concat("4"); it creates a new object because String s are immutable. When you change the original string it creates a new object,I agree that you are returning it but you are not catching it. So essentially what y

java的

这个问题在这里已经有了答案: Java是“通过引用传递”还是“按值传递”? 78个答案 因为当你打电话speak(name); ,当你做的时候,里面会说话 name = name.concat("4"); 它创建一个新的对象,因为String是不可变的。 当你改变原始字符串时,它会创建一个新的对象,我同意你正在返回它,但你没有捕捉到它。 所以基本上你在做什么是: name(new) = name(original) + '4'; // but you should notice that both the names are

Java Incremental operator query (++i and i++)

This question already has an answer here: Is Java “pass-by-reference” or “pass-by-value”? 78 answers Java is NEVER pass-by-reference, right?…right? [duplicate] 6 answers First of all you need to know the difference between x++ and ++X ; In case of x++ : First the current value will be used and it will be incremented next. That means you will get the present value of x for the operati

Java增量运算符查询(++ i和i ++)

这个问题在这里已经有了答案: Java是“通过引用传递”还是“按值传递”? 78个答案 Java从来没有通过引用,对吧?......对吧? [复制] 6个回答 首先你需要知道x++和++X之间的区别; 在x++情况下: 首先使用当前值,接下来会增加。 这意味着您将获得操作的x的当前值,并且如果下一次使用x将获得增加的值; 在++x情况下: 首先,当前值将递增,然后它将被用于(增加的值),这意味着您将在此操作中获取增加的值,并在