-1

I'm writing a code in which in its main method I create an object. But I don't know how to call its constructor. here is my code:

//This class calculate the greatest common divisor of two number 
import static java.lang.Math.min;
public class Calculate2
{
    private int num1,num2;
    public Calculate2(int num1 , int num2)
    {
        this.num1=num1;
        this.num2=num2;
    }
    public static void main(String[] args) 
    {
       Calculate2 gcd = new Calculate2();
       gcd.GCD();
    }
    public void GCD()
    {
        int min_num;
        int greatestcommondivisor=0;
        min_num = min(num1,num2);
        for(int commondivisor=1;commondivisor <= min_num;commondivisor++)
        {
           if(num1%commondivisor==0 &&  num2%commondivisor==0)
           {
               greatestcommondivisor=commondivisor;
           }
           commondivisor++;
        }
        System.out.println(greatestcommondivisor);
    }//End of the method gcd


}//End of the class calculate2

how should I call the constructor?

zahra
  • 31
  • 3
  • 7

4 Answers4

2

Constructors in a class are called right when we declare the object. Thus declare your object like this

Calculate2 obj = new Calculate2(1,2);
Ankit Rustagi
  • 5,539
  • 12
  • 39
  • 70
2
  1. Fact that you created your own constructor means that default one will not be added by compiler.
  2. Also your constructor looks like

    public Calculate2(int num1, int num2) {
    }
    

    which means it requires two int arguments, so you need to pass some int values to it like

    new Calculate2(42,24);
    
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • I want to get the num1 & num2 from user not some defults! – zahra Mar 16 '14 at 13:35
  • Then ask user to give you some values so you could you use them as constructors arguments. Take a look at http://stackoverflow.com/a/19532416/1393766 to see example how you can do it. – Pshemo Mar 16 '14 at 13:38
0

A constructor shares the same name as the class, so when u create a new object (gcd) like below

Calculate2 gcd = new Calculate2();

You have already called the default constructor of the class Calculate2

But since, you have declared a constructor with parameters:

public Calculate2(int num1 , int num2)

This means that the constructor of the class requires 2 parameters in it num1 and num2, so you have to call it like

Calculate2 obj_gcd = new Calculate2(100,200);
NoobEditor
  • 15,563
  • 19
  • 81
  • 112
  • But `Calculate2` doesn't have a default constructor since the OP has explicitly defined another constructor. – arshajii Mar 16 '14 at 13:36
  • @arshajii : patience is virtue mate.....wait for people to update their answer....if u dont feel its correct after first 5 mins, then drop a comment ( *learnt this through personal experience* )!! :) – NoobEditor Mar 16 '14 at 13:38
0

Your Constructor is parameteris constructor

  You must pass value to parameteris  Construcor
  You Can Call With differnt of type

  int i=10;
  int j=5;
  Calculate2 gcd = new Calculate2(i,j);

or

  new  Calculate2(10,20);

or

   Calculate2 gcd = new Calculate2(10,20);

or

    Calculate2 gcd;
    gcd = new Calculate2(15,20);
Benjamin
  • 2,257
  • 1
  • 15
  • 24