Ive Zenzerovic
Ive Zenzerovic

Reputation: 73

Methods with parameters

Is there a difference between declaring a variable in method parameters and declaring a variable inside a method? Both print out the same variable but i suppose there is a difference.

Upvotes: 1

Views: 95

Answers (4)

Randika Vishman
Randika Vishman

Reputation: 8124

And also by defining parameters within methods in Java (I guess the same applies for C++ too), which causes to the Methods Overloading.

Please look at the examples provided bellow:

 public void createPerson(int ID, String name) {
      // do something here
  }

  public void createPerson(double height, String name) {
      // do something here
  }

  public void createPerson(double height, String name, boolean testIt) {
      // do something here
  }

So what it helps to do is basically to change the method signature of the method, which leads the developer to implement different different method bodies within the 3 methods but with the same name but with different method signatures.

Read further about methods in the following link from Oracle Java.

Hope this is helpful to your question.

Upvotes: 0

MChaker
MChaker

Reputation: 2648

From Javadoc

Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.

Parameters ... Recall that the signature for the main method is public static void main(String[] args). Here, the args variable is the parameter to this method. The important thing to remember is that parameters are always classified as "variables" not "fields".

Upvotes: 0

StrongJoshua
StrongJoshua

Reputation: 995

If you're referring to what I think you are, then there is a big difference between the two. Take this for example:

public void printText() {
    System.out.println("Hello World");
}

This prints out the text Hello World. Now look at this method:

printText("Hello World");
public void printText(String text) {
    System.out.println(text);
}

The latter of these two examples offers much more flexibility because you can call the method with any parameter you like, whereas the former will only print Hello World, every time. Of course, depending on what you want the method to do, one form might be more appropriate than the other, but method arguments allow reusability.

Upvotes: 1

Sarkhan
Sarkhan

Reputation: 1291

by decalring variable as method parameter you can pass variables into method

public void printIt(String text){
   System.out.println(text);
}

but if you declare variable inside method like this:

public void printIt(){
   String name;
   //you can't pass 
}

Upvotes: 3

Related Questions