Method and parenthesis

Hi everyone,

I just wanted to ask a question regarding methods and parenthesis

So say I have code like this:
Jacks class {

int age = 29;
String name = Jack;

Public void randomMethod(Int ageNew, String namePerson) {

ageNew = age;
namePerson = person;

/my question is when I declared the variable age and name outside of this method, why would I need to use a new name in the parenthesis of this method and then attach those new parenthesis variables ageNew and namePerson to the variables outside the method, it’s just seems like going round the houses a bit and could just use the original variables name and age declared outside of the method, I’m sure theirs a reasonable explanation for this I just want to undertand it as it puzzles me?\

//and then I’d write a function here for the method

}

Public static void main(String args) {

}

}

Hi,

When declaring methods in java it’s really about being explicit. You are at the very least naming a method, specifying what type of info it will return (in the case of void it returns nothing). It’s one theory that good practice for methods is to be self-contained, meaning that they don’t change outside variables (this can be seen in extremes with functional programming, but even in oop like java I think it’s still good practice to an extent). They instead act as magic boxes that can process your variables and return new ones (keeping the old ones intact).

This example you show might not exemplify why it’s important, but as your code becomes more complex it starts to make more sense why this boundary could be important.

It is also the reason why global variables are often warned against in many languages. They are dangerous to use and really require the programmer to be fully aware of the the special-case circumstance in which it’s worth going through the trouble of using them.

Discussion on global variables: https://stackoverflow.com/questions/484635/are-global-variables-bad

1 Like

According to your code, you have written instant fields after class but you have not created the constructor. I have kept a simple example of your code which is quite complete, try to understand your problem with this example. If you don’t, point out where! Thanks! short Answer - You have to create new variables because you have to connect each thing to other to come up with a logical answer.
I created three steps, look carefully I have connected each new name variables wherever I needed. Finally It turned into a single result which you will get after running the code. Now if you ask me when I have to do this, its difficult because you have to understand the meaning and logic line by line from the start of your learning the java.

public class Jacks {

  int age;
  String name;

  public Jacks(int ageNew, String namePerson) {
    age = ageNew;
    name = namePerson;
  }

  public String randomMethod() { // Custom method
    return "My new student " + name + " and He is " + age;
  }

  public static void main(String[] args) {
    Jacks student = new Jacks(29, "Jack");
    System.out.println(student.randomMethod());
    
  }
}

Hi

Thanks for the reponse, and yes I can see how this could make things very complicated down the line, cheers for the explanation I undertand all points u made, appreciated​:slightly_smiling_face::pray:

Thank you for the clarification and the example very helpful and I now understand the concept more clearly :blush::pray: appreciated!!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.