class box {
int width;
int length;
int volume;
void volume(int height, int length, int width) {
volume = width * height * length;
}
}
class Prameterized_method {
public static void main(String args[]) {
box obj = new box();
obj.height = 1;
obj.length = 5;
obj.width = 5;
obj.volume(3, 2, 1);
System.out.println(obj.volume);
}
}
-
So what's the output of the code? I mean exactly what are you seeing?kolossus– kolossus2014-10-01 21:12:17 +00:00Commented Oct 1, 2014 at 21:12
-
2Your title has nothing to do with your question. Can you rephrase it?Qix - MONICA WAS MISTREATED– Qix - MONICA WAS MISTREATED2014-10-01 21:13:12 +00:00Commented Oct 1, 2014 at 21:13
-
1And there is no variable with name height in box. And also in java it is customary that class names starts with upper case, i.e. Box instead of boxNuri Tasdemir– Nuri Tasdemir2014-10-01 21:15:10 +00:00Commented Oct 1, 2014 at 21:15
-
5Just as a side note, that code is horribly bad for several reasons. Probably the worst is the volume method (that takes parameters, with names that are identical to existing instance variables, and doesn't return anything but sets an instance variable)??? Did you copy that example from some tutorial? If so, do yourself a big favor and find another tutorial.fvu– fvu2014-10-01 21:17:22 +00:00Commented Oct 1, 2014 at 21:17
-
And where did you find that code. There is no constructor in Box. volume function does not use instance variables which are set without a constructorNuri Tasdemir– Nuri Tasdemir2014-10-01 21:17:28 +00:00Commented Oct 1, 2014 at 21:17
4 Answers
If your question is whether it will print 25 (1*5*5) or 6(1*2*3), then it will print 6. In the code in your method:
void volume(int height, int length, int width) {
volume = width * height * length;
}
volume represents this.volume, whereas height, length and width are local to the function. To access the instance variable, you would have to explicitly type this.volume.
Hence this.volume will be the return value of volume(3,2,1) and does not have any relation to this.height, this.length or this.width.
Comments
Since there is not access modifier specified it is package private. Means you can access it from outside and don't need any getters for them.
box obj = new box(); // Creates an object of type box
obj.volume(3, 2, 1); // Call the method to set the volume of the box with the parameters.
System.out.println(obj.volume); // Prints the volume
The are all connect by the obj object that we created in the first step.
Comments
The sample of code shown above does as following in following sequence.
The program startes in the
public static void main(String args[]){
...
}
Block. Then the code inside is executed. The box obj = new box(); line creates a new object. The object is of type box which is defined above. This means that the object has the fields and methods as defined in the box section class box { ... }
The next few lines of code
obj.height = 1;
obj.length = 5;
obj.width = 5;
obj.volume(3, 2, 1);
System.out.println(obj.volume);
When obj.height we are setting the height of the box to 1
When obj.length is called we are setting the length of the box to 5
When obj.width is called we are setting the width of the box.
Please not that the box class does not have a height field, you need to add that
class box{
int width;
int heigh;
int length;
int volume;
...
}
Then we have a method call. This is the obj.volume(3,2,1) call. This takes a box with dimensions 3x2x1 and calculates the volume by multiplying all the values. This is done a bit inefficiently as we are not using the fields we set earlier. You could modify the method as this:
void volume() {
volume = width * height * length;
}
This method will use the values we already set on the object instead of using the values set by the parameters. This will enfore that the volume will not be calculated with values different from the ones we set earlier.
The last line of code
System.out.printn(obj.volume)
will print out the volume of the box to the screen