2

Let's say I have a class and I want one of its methods to behave differently depending on an argument. What I want to do is something like this (I know this doesn't work in Java):

class myClass(int a) {
    public void myMethod() {
        if (a == 1)
            // do something
        if (a == 2)
            // do something else
    }
}

Is there a way to do this?

4 Answers 4

6

You have two ways to do that.

  • Pass the argument to the class contructor, and keep a local reference to it. You can then use it in all the methods of your class.

Like this:

class MyClass {
    private final int a;

    public MyClass(int a) {
        this.a = a;
    }

    public void myMethod() {
        if (a == 1)
            // do something
        if (a == 2)
            // do something else
    }
}
  • Pass the argument to the method, if you intend to use it only in that method.

Like that:

class MyClass {
    public void myMethod(int a) {
        if (a == 1)
            // do something
        if (a == 2)
            // do something else
    }
}  
Sign up to request clarification or add additional context in comments.

4 Comments

It depends if that's just an argument to the method, or something that needs to be reused multiple times within the class
Thank you, this works. But when I declare it as "private final int", it says "cannot assign a value to final variable hashType". So I declared it as a variable. Maybe I was doing something wrong?
final only means that once the value is assigned in the constructor (or at declaration time), it cannot be changed. It's not necessary, and it will not work if indeed you try to re-allocate it later. Private is good and should be used, to preserve encapsulation.
@hattenn: each keyword in java has specific properties so it's better to know the properties before using.
4

The argument must be passed to the method, not to the class:

class MyClass { // note that class names should start with an uppercase letter.
    public void myMethod(int a) {
        if (a == 1)
            // do something
        if (a == 2)
            // do something else
    }
}

Read the Java language tutorial.

3 Comments

Class names "should" not start with an uppercase, it's just good practice to follow the Sun naming standard.
And these good practices "should" be followed, thus class names "should" start with an uppercase letter. If it was an absolute rule, I would have said "must", and not "should".
Oh well, ok, English is not my native language ;)
2

You can pass this argument in constructor

class MyClass {
    private int a;
    public MyClass(int a){
        this.a = a;
    }
    public void myMethod() {
        if (a == 1)
            // do something
        if (a == 2)
            // do something else
    }
}

Comments

1

Polymorphism is the way to go. Define your base class first with an abstract method myMethod() and then extend it with two classes providing two different implementations of the method.

1 Comment

Oooh no it's not! Inheritance is evil and will most likely produce unreadable code, and difficult to maintain and test. Composition over inheritance, anytime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.