English 中文(简体)
Why is the output this? (Java)
原标题:
  • 时间:2009-11-20 03:52:57
  •  标签:
  • java
  • object

This is an instance method from a Rectangle class where we modify the x and y coordinates of the rectangle and its width and height

public void modify(int newX, int y, int width, int h) {
    int x = newX;
    this.y = y;
    width = width;
    this.height = height;
}

Rectangle r3 = new Rectangle(0, 0, 10, 10);
r3.modify(5, 5, 50, 50);
System.out.print(r3.getX() + " " + r3.getY() + " ");
System.out.println(r3.getWidth() + " " + r3.getHeight());

I have this code and I know that the output is 0 5 10 10 but i m not entirely sure why. can anyone explain why?

最佳回答
public void modify(int newX, int y, int width, int h) {
    int x = newX;  // the value isn t saved to the class members
    this.y = y; // this is saved, hence you see the change in the y value
    width = width; // meaningless, the variable is overwritten with it s own value
    this.height = height; // who is height? the function receives h
}
问题回答

You have created a new object of type "int" for X within the modify method. This means that it only exists within that method since you re not passing it by reference. So, the newX value is only 5 within the modify method, but does not exist as 5 outside of it. this.y works fine because you ve called that specific instance of the object and modified it s value. Therefore, it s retained outside the method. width = width doesn t work because you re simply assigning 50=50 (since you ve inputted 50 as the width). this.height = h would be fine, but you ve said this.height = height . But, from the code you ve given, height doesn t exist.

y is the only instance variable that is actually modified in the modify method. The other the arguments passed in have no net effect on the state of the object.

Actually, the code shouldn t compile. height isn t defined in your method call. Unless this is another property that you didn t include in your code snippet.

int x = newX creates a new int named x that you then do nothing with. That s why r3.getX() returns 0, since you never modified it.

this.y = y changes the value of the field y within the Rectangle class. This is why this change is shown in your output as 5.

width = width changes the method parameter named width to itself. It doesn t change the value, but it also doesn t set the field width within Rectangle. No change shown, original value of 10 prints.

If height is a field elsewhere, then it makes sense that r3.getHeight() wouldn t update the field, since the parameter in the method call is for h, not height. If not, then I don t know how the code compiles since height isn t mentioned anywhere.

The "int x = newX" line creates a variable "x" on the stack that exists only for the duration of the current method call.

"this.x" would refer to the "x" created by the classes constructor. Which is probably what "getX()" returns.

This code shows the difference between the function stack variable and object variable. For function modify, the four passing variables are on the stack. The line declares a stack variable x and set its value as newX. The second line uses the object variable this.y and set to passing variable y. The third line is to assign the width to its self on stack. The fourth line uses the object variable height and assign to its self. Once the program goes out of the scope of function modify, all its stack variables value are whacked. So the result is 0 5 10 10 because only the second line which is not stack variable this.y retains its value after calling function modify.

I would venture to say your issue is in how you are assigning the new values of x, y, width and height to your rectangle object.

Assuming that your modify method is in the rectangle class your code currently looks like this (I added comments on the mistakes:

public void modify(int newX, int y, int width, int h) {
    int x = newX;   //you are declaring a new x here...not assigning newX to rectangle s x 
    this.y = y;     //this is correct
    width = width;          //here you re just assigning the parameter width its current value
    this.height = height;   //here you are assigning the rectangles height value to itself
}

I would HIGHLY advise finding a naming convention and sticking with it as it would help tremendously here.

Try something like this:

public void modify(int x, int y, int w, int h) {  //changed names of parameters
    this.x = x;       //removed the int type declaration and used this. prefix 
    this.y = y;       //changed nothing
    this.width = w;   //adjusted for renamed parameter, used this. prefix
    this.height = h;  // adjusted for renamed parameter, again used this. prefix
}

As you can see, sticking to a convention makes the code less confusing and easier to read. This also allows you to see your mistakes more easily as they will usually stick out from your convention like a sore thumb. Don t worry it comes with practice.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签