简体   繁体   中英

Should I declare variables in the class, if the class has only a constructor?

Let's say I have a class:

public class Foo {
    private int x;
    private int y;
    public Foo(int x, int y) {
        this.x = x;
        this.y = y;
        /**
         * here comes a lot of code within this constructor
         * that uses these x and y variables.
         */
    }
}

I understand, that if I had some other methods in this class that would use the x and y variables, I should have copied the constructor's arguments into the fields like I did above, but what if I don't have any other methods but only the constructor? Should I still create the fields or my class should look like this:

public class Foo {
    public Foo(int x, int y) {
        /**
         * here comes a lot of code within this constructor
         * that uses these x and y variables.
         */
    }
}

What is the right style?

UPDATE

Sorry, I was wrong by thinking that there is a some general pattern of doing this, that's why I gave some idle example. I still will not copy-paste my code, because the project is too big and a single class out of context will not make any sense. But here's a more specific example:

import javax.swing.JLabel;
import javax.swing.JPanel;

public class InformationPanel extends JPanel {
    private JLabel nameLbl = new JLabel();

    public InformationPanel(Student student) {
        nameLbl.setText(student.getName());
        this.add(nameLbl);
    }

}

So in this case should I have a local field

private Student student;

And then:

this.student = student;

Or I shouldn't?

A constructor is used to initialize the state of an object. Based on your question, your object has no state.

Your constructor accepts some parameters which are only used inside the constructor. Your constructor doesn't produce any output. Although I don't know what logic you have inside this constructor, it seems like you are using the constructor as if it was a static method. Why not put your logic in a static method?

The first is the preferred style. However, the answer for this case is probably: use a static method, don't use a constructor.

If you want just the functionality that's encoded in the constructor method, then a static method is the way to go.

As a constructor is used to create a new instance of a class, which combines functionality (via other methods) and state (via fields) it's hard to see how a stateless, methodless instance would be much use. Hence the guess on my part is that what you're after is:

public class Foo {

    public static void doSomething(int x, int y) {
       /**
         * here comes some code within this method
         * that uses these x and y variables.
         */
    }
}

The last part being - don't put a lot of code in a single method, unless it really belongs there. Think modularly and decompose the method into parts each of which may be tested.

You should not store the values in fields if they are only used in the constructor.

However, more importantly what you are doing is not appropriate for a constructor. As the resulting object created seems fairly useless.

Don't see why you need a class to achieve what you are trying to do. Depending on your use case it either should be a method in another class or a Utility Class containing static methods. But it all depends on what you are trying to do or the context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM