-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);

    }
}
kapex
  • 28,903
  • 6
  • 107
  • 121
user3453661
  • 33
  • 1
  • 7
  • All you need are some basic tutorials. Yes it's a constructor btw. – keyser Dec 08 '14 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 – Richard Tingle Dec 08 '14 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 – ControlAltDel Dec 08 '14 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. – Jeroen Vannevel Dec 08 '14 at 20:05

2 Answers2

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();
La-comadreja
  • 5,627
  • 11
  • 36
  • 64
  • 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. – user3453661 Dec 08 '14 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). – La-comadreja Dec 08 '14 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();
Jason S
  • 184,598
  • 164
  • 608
  • 970