elGreato
elGreato

Reputation: 140

Difference between methods that take parameters and the ones don't?

Ok, this might be a stupid question. However, it really confuses me;
What is the difference between a method like :

 toString()  

and a method like
toString(String s) ?

I know the last one doesn't exist but let's say i made one like this.
Why should I choose to make a method that can be invoked like this: object.method(); and not: method(object j ); i hope i could explain myself.

Upvotes: 1

Views: 233

Answers (3)

npinti
npinti

Reputation: 52185

It all depends on what you want the method to do.

For instance, the .toString(), is used to provide a string representation of the object. In this case, all the data which the method requires to operate as expected is stored within the object itself, thus, no external input is required.

Taking a different example, printToStream(Stream stream) (where Stream would be some interface used by all the streams, be it file streams, console streams, network streams, etc). In this case, the method needs to know to which stream it must write to.

The class to which the printToStream method belongs could have a property which denotes to which stream it must print, and thus allowing us to change the signature of printToStream to printToStream(), but that would require 2 lines of code to set up, and in this case, this can potentially introduce problems, especially if you need to print to different streams and at some point, you forget to set the property. Thus, in this case, having the method take in the extra parameter results in cleaner code which is less error prone.

EDIT: The parseInt method is a static method which is provided by the Integer class which is used to transform items from one type to integer. Static methods are methods which need to be self contained, so if you want to transform one object from one form to another using a static method, passing it as a parameter is the way to go.

The suggestion you are making would take the look of something like so: int a = "123".ToInteger();. I guess that that would be one way to go around doing the same thing, but in the end the Java specification was not designed with that frame of mind.

EDIT 2: I'll try and expand my answer the one provided by @ user3284549. Please note that my answer is in no means final and should be supplimented by other material you can find online. In Java, methods are either:

  • Static. Static methods are methods which have no state (more on that later) and are self contained. As seen earlier, the .parseInt method from the Integer class is one example of such method. Another examples are provided by the Math class.

  • Non Static. Non static methods are methods which are made available once that an object is initialized (or instantiated). These methods are methods which act upon the state of the object, or else, expose some functionality which the object provides which might, in turn, affect how it behaves.

Taking your dice example, again, you can achieve this in two ways:

Let us assume that we have a class called Dice:

public class Dice {
    private int value;

    public int getValue() {
        return this.value;
    }

    public void setValue(int value) {
        this.value = value;
    }
}

Static method

public static void roll(Dice d) {
    //Rolling the dice entails a potential change in it's current value. Thus, you would need to access the dice itself and update the value.
    Random r = ...
    d.setValue(randomNumber);
}

Non Static Method

We just enhance the Dice class to have a new method which mimics a dice roll as follows:

public void roll() {
    Random r = ...;
    this.value = randomNumber;
}

If you notice, even in the static method, we still made use of a method which takes in a parameter. The particular setXXX methods (and their counter parts getXXX methods provide us with encapsulation). This is because we need to change the state of the Dice object.

Upvotes: 2

Razib
Razib

Reputation: 11173

Lets proceed with you example -

object.method();
object.method(Object j);  

The first one does some operations on it's variable and returns/set some value (although there may be a lot of other cases, but for simplicity just consider these). When the method is toString() it actually represents a string representation of some important properties of the Object. In this case the toString() method don't need to be feed some argument from the outside of the object.

And when you use object.method(Object j) it means you need to provide some arguments to complete it's task.

Upvotes: 1

user3275863
user3275863

Reputation: 65

For the second part of your question. Think about encapsulation. If the method needs access to data that is private to the object then object.method() would be the correct way. And also it is probably not going to be helpful in processing objects of any other type.

For the second type, it could be a static method or utility method commonly used on many different objects. The method implementation logic doesn't have to be tightly related to the object's internal implementation.

Upvotes: 1

Related Questions