0

For whatever reason, I cannot find this question anywhere else, nor can I find the answer online. If I have the following:

package temp1;

public class MainClass {

    public static void main(String[] args) {
    }

    public MainClass(int radius_x, int area_x, int circumference_x) {
        int radius = radius_x;
        int area = area_x;
        int circumference = circumference_x;
    }

}

Assuming that this is even correct usage, then how would I actually use the variables defined in the constructor here? They only work inside of the constructor thanks to scope.

1
  • 4
    You declare them in a larger scope. Commented Nov 2, 2013 at 22:50

4 Answers 4

2

You are correct that the code you supply makes little sense. A more common scenario is to use the constructor to initialize a few instance variables, which can then be used throughout the class.

public class MainClass {
    private int radius;
    private int area;
    private int circumference;

    public static void main(String[] args) {
    }

    public MainClass(int radius_x, int area_x, int circumference_x) {
         this.radius = radius_x;
         this.area = area_x;
         this.circumference = circumference_x;
    }

}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, this makes much more sense. I was wondering because I am following an absolutely horrible textbook for Java in BlueJ because of a class in my highschool. The textbook wanted me to define the variables in the constructor without declaring them anywhere else and doesn't show me how to use the variables afterwards. I had the feeling it was doing something wrong, but I wanted to clarify. In an earlier chapter it explicitly stated that do while and while were the same and had no advantages/disadvantages, so I'm not surprised to it's wrong again. :)
@Stopforgettingmyaccounts... out of curiosity, could you please provide the textbook's title or ISBN?
Blue Pelican Java by Charles E. Cook.
Excerpt: "The do-while loop: A do-while loop is exactly the same as a while-loop except the control expression is at the bottom of the loop rather that at the top as it is with the while-loop." (page 58 of the PDF document)
@Stopforgettingmyaccounts... well that's not incorrect, just potentially very confusing. Anyhow, googled it, read through some of the pdf and yes, doesn't look peachy. Hang in there :(
|
0

One way to re-use the arguments of your constructor is to have instance variables assigned with your constructor's arguments' values.

As such:

package temp1;

public class MainClass {

    private int radiusX;
    private int areaX;
    private int circumferenceX;

    public static void main(String[] args) {
    }

    public MainClass(int radius_x, int area_x, int circumference_x) {
        this.radiusX = radius_x;
        this.areaX = area_x;
        this.circumferenceX = circumference_x;
    }
    // TODO getters [and setters] for instance variables
}

Comments

0
package temp1;

public class MainClass {

        int radius ;
        int area;
        int circumference;

    public static void main(String[] args) {
    }

    public MainClass(int radius_x, int area_x, int circumference_x) {
        this.radius = radius_x;
        this.area = area_x;
        this.circumference = circumference_x;
    }

}

Comments

0

You created a constructor, but the variables inside are only local to the constructor itself and not outside of it. To do this, you need class member fields:

public class MainClass {
    private int radius;
    private int area;
    private int circumference;

    public static void main(String[] args) throws Exception {
        MainClass m = new MainClass(5, 6, 7);
        System.out.println("The radius is " + m.getRadius());
    }

    public MainClass(int radius_x, int area_x, int circumference_x) {
         radius = radius_x;
         area = area_x;
         circumference = circumference_x;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public int getArea() {
        return area;
    }

    public void setArea(int area) {
        this.area = area;
    }

    public int getCircumference() {
        return circumference;
    }

    public void setCircumference(int circumference) {
        this.circumference = circumference;
    }
}

Keep in mind also there is no need to pass in area_x and circumference_x Those can be derived from radius_x. That's assuming you are being faithful to the meanings of the terms rather than just playing around with variables to learn the language.

1 Comment

area_x and circumference_x were intended to be used with methods that aren't written here, such as radiusFromArea and areaFromCircumference, etc. in case one or the other weren't known.