-1

I am not sure if you see what I am trying to do here but basically I have a few questions and problems

1)The part that is called public MethodPractice() ... what is this called? is this considered a constructor a method or what?

2)The part named MethodPracticeDiff() . . . is this allowed and if so how do I insert it into the main method for execution... Do you guys see what I am trying to do here? Basically I want to split the program up into different pieces for example let say I wanted my own space for a calculation method to add to numbers up and another method to define the numbers like give them a value and a last method with a for loop making the numbers printout 10times

Any who before I make this more confusing than what it is, my question is how do I make this program execute

public class MethodPractice {
    public static void main (String[]args){

        MethodPractice add = new MethodPractice();
        //MethodPracticeDiff add2 = new MethodPracticeDiff();


    }

    public MethodPractice() {
        int x = 0;
        int y = 99 ;
        int total = x + y;
        System.out.println(total);


    }
    public void MethodPracticeDiff(){
        int z = 10;
        int k = 25;
        int total = z + k;
        System.out.println(total);

    }
}
4
  • All you need are some basic tutorials. Yes it's a constructor btw.
    – keyser
    Dec 8, 2014 at 20:03
  • Yes, that looks like a constructor and the search term you want is method (and constructor) overloading. You don't put Diff at the end, as long as the methods/constructors take different arguments java figures it out Dec 8, 2014 at 20:03
  • 1
    This is a very basic question. You'd be better off searching for a tutorial on getting started with Java to get a feel for how the language works Dec 8, 2014 at 20:05
  • 4
    This question appears to be off-topic because it is a too broad subject and not fit for a Q&A format. Stack Overflow helps with specific problems, it does not teach you the language. Dec 8, 2014 at 20:05

2 Answers 2

1

(1) If it's in class MethodPractice, it's a constructor.

(2) Yes, this is allowed. But it's a method not a constructor. Standard practice is to begin it with a lowercase letter.

As follows in the main() method:

MethodPractice add = new MethodPractice();
add.methodPracticeDiff();
2
  • Great thank you so much so can you help me this... do you lowercase it because it is a method? and is MethodPractice considered a constructor because it is part of the MethodPractice class? and I see what you did now add.methodPracticeDiff(); so basically i can add another method again and all i will have to do is add.nameOfNewMethod(); ? oh yes and last question why does MethodPracticeDiff has to be void? I thought void means it does not output any value? If you can answer any questions i do not mind thank you. Dec 8, 2014 at 20:26
  • You lowercase methodPracticeDiff because it is a non-constructor method (constructors are a special type of method). MethodPractice is a constructor because it initializes an instance of the MethodPractice class. methodPracticeDiff would be void if you don't want to return anything (if you want to return something, it wouldn't be void). Dec 8, 2014 at 20:59
1

MethodPractice() is a constructor -- it has no return value and matches the name of the class.

MethodPracticeDiff() is a method -- it has a return value and does not match the name of a class.

You call methods once you have an instance of the class. e.g.

MethodPractice add = new MethodPractice();
add.MethodPracticeDiff();
2
  • and, it should start with a lowercase letter
    – keyser
    Dec 8, 2014 at 20:19
  • Thank so much for the super fast response, and yes it did work Dec 8, 2014 at 20:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.